7.11. Validate an XML Document Against a Schema

Problem

You need to validate the content of an XML document by ensuring that it conforms to an XML schema.

Solution

Since LINQ to XML has not added any new or direct support for working with XML schemas, you need to rely on the more general functionality found in the System.Xml namespace. To use XML schemas, you should call XmlReader.Create and supply an XmlReaderSettings object that indicates you want to perform validation. Then move through the document one node at a time by calling XmlReader.Read, catching any validation exceptions. To find all the errors in a document without catching exceptions, handle the ValidationEventHandler event on the XmlReaderSettings object given as a parameter to ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.