Validating Documents with XSD

XML Schema Definition (XSD) is an XML dialect that describes the format, data types, and constraints of the information contained in an XML file. Countless online references on the XSD specification itself are available, as well as many publications.

This section shows you how to validate an XML document based on an existing XSD. To show this, we will create an XSD that defines the format of the items.xml document. Without knowing all that much about XSD, you can still easily create schemas by inferring them from instance documents such as items.xml with a command-line utility that ships with the .NET Framework SDK: XSD.EXE.

You can execute XSD.EXE against your instance document, and then modify it using the designer inside Visual Studio to change data types and relationships. The visual designer included with Visual Studio 2005 is an excellent tool for designing XML schemas.

Listing 8.5 contains the results of executing XSD.EXE as well as a modification to indicate that the ID of an item is an integer.

Listing 8.5. items.xsd

<?xml version="1.0" encoding="utf-8"?> <xs:schema  xmlns="" xmlns:xs="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">   <xs:element name="items" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">     <xs:complexType>       <xs:choice minOccurs="0" maxOccurs="unbounded">         <xs:element name="item">           <xs:complexType>             <xs:sequence>               <xs:element name="attribute" minOccurs="0" maxOccurs="unbounded">                 <xs:complexType>                   <xs:attribute name="name" type="xs:string" />                   <xs:attribute name="value" type="xs:string" />                 </xs:complexType>               </xs:element>               <xs:element name="specials" minOccurs="0" maxOccurs="unbounded">                 <xs:complexType>                   <xs:sequence>                     <xs:element name="special" minOccurs="0" maxOccurs="unbounded">                        <xs:complexType>                          <xs:attribute name="name" type="xs:string" />                          <xs:attribute name="description" type="xs:string" />                        </xs:complexType>                     </xs:element>                   </xs:sequence>                 </xs:complexType>               </xs:element>             </xs:sequence>             <xs:attribute name="id" type="xs:integer" />             <xs:attribute name="name" type="xs:string" />             <xs:attribute name="description" type="xs:string" />           </xs:complexType>         </xs:element>       </xs:choice>     </xs:complexType>   </xs:element> </xs:schema> 

Now take a look at the code that validates an instance document against a schema:

static void Main(string[] args) {     XmlDocument items = new XmlDocument();     items.Load(@"..\..\..\..\items.xml");     XmlSchema schema = XmlSchema.Read(         new FileStream(@"..\..\..\..\items.xsd",FileMode.Open), new ValidationEventHandler(OnSchemaValidate));     items.Schemas.Add(schema);     items.Validate(new ValidationEventHandler(OnValidate));     Console.ReadLine(); } static void OnValidate(object sender, ValidationEventArgs vargs) {     Console.WriteLine(vargs.Message); } static void OnSchemaValidate(object sender, ValidationEventArgs vargs) {     Console.WriteLine(vargs.Message); } 

The preceding code adds an XmlSchema instance to the document itself. When the Validate method is called, the validation takes place in the background, and each time a validation error occurs, the OnValidate method is invoked. To test this code, go back to the original items.xml code and change one of the item IDs from a number to something with letters in it. When you run the code, you'll see the following error message:

The 'id' attribute is invalid - The value 'abc' is invalid according to its data type ''  The string 'abc' is not a valid Integer value. 

Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298 © 2008-2017.
If you may any questions please contact us: