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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.