Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

XML Schema (XSD) Validation with Multiple Schemas 

You can use the XmlValidatingReader to validate XML documents against XML Schema definition language (XSD) schemas from multiple schemas.


The XmlValidatingReader class is obsolete in the Microsoft .NET Framework version 2.0. You can create a validating XmlReader instance using the XmlReaderSettings class and the Create method. For more information, see Validating XML Data with XmlReader.


The following code example creates an XmlValidatingReader that takes a file stream, Mixed.xml, as input and validates XML fragments against multiple schema sources. The XmlValidatingReader constructor takes a stream of an XML fragment, an XmlNodeType, and XmlParserContext.


In the following example, the top-level element is not validated. The XmlValidatingReader throws a warning for the root element if ValidationType is set to ValidationType.Schema.

using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

namespace ValidationSample
   class Sample
      public static void Main()
         FileStream stream = new FileStream("Mixed.xml", FileMode.Open);
         XmlValidatingReader vr = new XmlValidatingReader(stream, XmlNodeType.Element, null);

         vr.Schemas.Add(null, "Book.xsd");
         vr.Schemas.Add(null, "Tape.xsd");
         vr.ValidationType = ValidationType.Schema;
         vr.ValidationEventHandler += new ValidationEventHandler (ValidationHandler);

         Console.WriteLine("Validation finished");

      public static void ValidationHandler(object sender, ValidationEventArgs args)
         Console.WriteLine("***Validation error");
         Console.WriteLine("\tSeverity:{0}", args.Severity);
         Console.WriteLine("\tMessage:{0}", args.Message);

The following outlines the contents of an XML Schema file, Book.xsd, to be validated against.

<xs:schema xmlns="urn:bookstore-schema"
   <xs:element name="book">
            <xs:extension base="xs:string">
               <xs:attribute name="price" type="xs:decimal" />

The following outlines the contents of an XML Schema file, Tape.xsd, to be validated against.

<xs:schema xmlns="urn:tapestore-schema"
   <xs:element name="tape" type="xs:string"/>

The following outlines the contents of the XML stream, Mixed.xml, containing the XML fragments to be validated.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:dvdstore-schema" targetNamespace="urn:dvdstore-schema">
    <xs:element name="dvd" type="xs:string" />
  <pb:book price="7.99" xmlns:pb="urn:bookstore-schema">The Autobiography of Benjamin Franklin</pb:book>
  <pd:dvd xmlns:pd="urn:dvdstore-schema">The Godfather</pd:dvd>
  <pt:tape xmlns:pt="urn:tapestore-schema" xsi:schemaLocation="urn:tapestore-schema tape.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Il Postino</pt:tape>

See Also

Community Additions

© 2015 Microsoft