Schemas Property

XmlValidatingReader.Schemas Property


Gets a XmlSchemaCollection to use for validation.

Namespace:   System.Xml
Assembly:  System.Xml (in System.Xml.dll)

public XmlSchemaCollection Schemas { get; }

Property Value

Type: System.Xml.Schema.XmlSchemaCollection

The XmlSchemaCollection to use for validation.


The XmlValidatingReader class is obsolete in .NET Framework 2.0. You can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. For more information, see the Remarks section of the XmlReader reference page.

The XmlSchemaCollection holds pre-loaded XML-Data Reduced (XDR) and XML Schema definition language (XSD) schemas. This property gives the reader access to the cache of schemas and allows it to validate without having to re-load schemas every time. The reader does not add anything to the XmlSchemaCollection.

If the XmlSchemaCollection includes an XML schema (XSD) which contains include or import elements that reference other namespaces, the schemas for these other namespaces are loaded for validation purposes only. Unless these schemas were explicitly added to the schema collection, they will not be accessible using any of the collection methods or properties. For example, if the collection contains the schema file a.xsd, which contains a reference to the schema file b.xsd, you must add b.xsd to the schema collection before you can access it using any of the collection methods or properties.

When the XmlSchemaCollection is accessed using the Schemas property, the XmlSchemaCollection.Add method uses the XmlResolver specified by the XmlValidatingReader.XmlResolver property.


The schemas must be added to the XmlSchemaCollection before the first Read call.

The following example validates three XML files using schemas stored in the XmlSchemaCollection.

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

public class SchemaCollectionSample
  private const String doc1 = "booksSchema.xml";
  private const String doc2 = "booksSchemaFail.xml";
  private const String doc3 = "newbooks.xml";
  private const String schema = "books.xsd";
  private const String schema1 = "schema1.xdr";

  private XmlTextReader reader=null;
  private XmlValidatingReader vreader = null;
  private Boolean m_success = true;

  public SchemaCollectionSample ()
    //Load the schema collection.
    XmlSchemaCollection xsc = new XmlSchemaCollection();
    xsc.Add("urn:bookstore-schema", schema);  //XSD schema
    xsc.Add("urn:newbooks-schema", schema1);  //XDR schema

    //Validate the files using schemas stored in the collection.
    Validate(doc1, xsc); //Should pass.
    Validate(doc2, xsc); //Should fail.   
    Validate(doc3, xsc); //Should fail. 


  public static void Main ()
      SchemaCollectionSample validation = new SchemaCollectionSample();

  private void Validate(String filename, XmlSchemaCollection xsc)

     m_success = true;
     Console.WriteLine("Validating XML file {0}...", filename.ToString());
     reader = new XmlTextReader (filename);

     //Create a validating reader.
    vreader = new XmlValidatingReader (reader);

     //Validate using the schemas stored in the schema collection.

     //Set the validation event handler
     vreader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
     //Read and validate the XML data.
     while (vreader.Read()){}
     Console.WriteLine ("Validation finished. Validation {0}", (m_success==true ? "successful" : "failed"));

     //Close the reader.


  private void ValidationCallBack (object sender, ValidationEventArgs args)
     m_success = false;

     Console.Write("\r\n\tValidation error: " + args.Message);


The sample uses the following five input files:






.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft