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?
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.
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With