tags:

views:

1391

answers:

3

Input is xml as a string and I have a XSD schema to verify against.

I wish to validate the xml against the XSD schema while loading the xml into an XDocument (requirement since I need to do parsing with Linq afterwards).

Anyone has a small code-snippet?

+4  A: 

Load the document, and then use XDocument.Validate, passing in an XmlSchemaSet to represent the schemas you want to validate against, and a ValidationEventHandler to react appropriately to any validation problems.

There's an MSDN article with a complete example and discussion.

Jon Skeet
+1  A: 

You can use the above method. Here is another link for code snippet Safari Books

Syam
+1  A: 

Just a note about the above answer. It is correct but can be confusing. XDocument.Validate is an extension method that exists in the System.Xml.Schema namespace. If you do not use this namespace then the Validate method will not appear in intellisense. Just thought I'd help those who were as confused as I was. :)

Jordan