This documentation is archived and is not being maintained.

XmlSchemaCollection Class

Contains a cache of XML Schema definition language (XSD) and XML-Data Reduced (XDR) schemas. This class cannot be inherited.

For a list of all members of this type, see XmlSchemaCollection Members.


[Visual Basic]
NotInheritable Public Class XmlSchemaCollection
   Implements ICollection, IEnumerable
public sealed class XmlSchemaCollection : ICollection, IEnumerable
public __gc __sealed class XmlSchemaCollection : public
   ICollection, IEnumerable
public class XmlSchemaCollection implements ICollection,

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


Schemas are loaded using the Add method, at which time the schema is associated with a namespace URI. For XML Schemas, this will typically be the targetNamespace property of the schema.

Although this class stores both XML Schemas and XDR schemas, any method and property that takes or returns an XmlSchema applies to XML Schemas only.

This version of the product supports the World Wide Web Consortium (W3C) XML Schema recommendation located at and An XML Schema must reference the W3C Schema namespace, in its schema element. See the Add method for an example.

XmlSchemaCollection can be used by XmlValidatingReader for efficient data validation.


[Visual Basic, C#, C++] The following example validates an XML document using the XmlSchemaCollection.

[Visual Basic] 
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO

public class ValidXSD 

  public shared sub Main() 
    Dim sc as XmlSchemaCollection = new XmlSchemaCollection()
    AddHandler sc.ValidationEventHandler, AddressOf ValidationCallBack
    sc.Add(nothing, "books.xsd")

    if(sc.Count > 0)
      Dim tr as XmlTextReader = new XmlTextReader("notValidXSD.xml")
      Dim rdr as XmlValidatingReader = new XmlValidatingReader(tr)

      rdr.ValidationType = ValidationType.Schema
      AddHandler rdr.ValidationEventHandler, AddressOf ValidationCallBack
      while (rdr.Read())
      end while
    end if

  end sub

  private shared sub ValidationCallBack(sender as object, e as ValidationEventArgs) 
    Console.WriteLine("XSD Error: {0}", e.Message)
  end sub

end class

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

public class ValidXSD {

  public static void Main() {
    XmlSchemaCollection sc = new XmlSchemaCollection();
    sc.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
    sc.Add(null, "books.xsd");

    if(sc.Count > 0)
      XmlTextReader tr = new XmlTextReader("notValidXSD.xml");
      XmlValidatingReader rdr = new XmlValidatingReader(tr);

      rdr.ValidationType = ValidationType.Schema;
      rdr.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
      while (rdr.Read());

  private static void ValidationCallBack(object sender, ValidationEventArgs e) {
    Console.WriteLine("Validation Error: {0}", e.Message);

#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;
using namespace System::IO;

public __gc class ValidXSD {
  static void main() {
    XmlSchemaCollection* sc = new XmlSchemaCollection();
    sc->ValidationEventHandler += new ValidationEventHandler(0, ValidationCallBack);
    sc->Add(0, S"books.xsd");

    if(sc->Count > 0)
      XmlTextReader* tr = new XmlTextReader(S"notValidXSD.xml");
      XmlValidatingReader* rdr = new XmlValidatingReader(tr);

      rdr->ValidationType = ValidationType::Schema;
      rdr->ValidationEventHandler += new ValidationEventHandler(0, ValidationCallBack);
      while (rdr->Read());

  static void ValidationCallBack(Object* /*sender*/, ValidationEventArgs* e) {
    Console::WriteLine(S"Validation Error: {0}", e->Message);

int main()

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Xml.Schema

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

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

See Also

XmlSchemaCollection Members | System.Xml.Schema Namespace