
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Validating XML
|
839
Put them all together and you get all of the married female participants who were
witnesses.
Once you have an
XPathNavigator, call the Select method on it, passing the XPath-
based query to select the nodes you are looking for. These are returned via the
XPathNodeIterator. You use the XPathNodeIterator to write out the names of the par-
ticipants you found and close the
MemoryStream.
See Also
See the “XPathDocument Class,” “XPathNavigator Enumeration,” and “XPathNode-
Iterator Class” topics in the MSDN documentation.
15.4 Validating XML
Problem
You are accepting an XML document created by another source and you want to ver-
ify that it conforms to a specific schema. This schema may be in the form of an XML
schema (XSD or XML—XDR); alternatively, you want the flexibility to use a docu-
ment type definition (DTD) to validate the XML.
Solution
Use the XmlReaderSettings to create an XmlReader that can validate XML documents
against any descriptor document, such as an XSD, a DTD, or an XDR, as shown in
Example 15-3.
Example 15-3. Validating XML
public static void ValidateXml( )
{
// Create XSD schema collection with book.xsd.
XmlReaderSettings settings = new XmlReaderSettings( );
// Wire up handler to get any validation errors.
settings.ValidationEventHandler += settings_ValidationEventHandler; ...