Export (0) Print
Expand All

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.

System.Object
   System.Xml.Schema.XmlSchemaCollection

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

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.

Remarks

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 http://www.w3.org/TR/xmlschema-1 and http://www.w3.org/TR/xmlschema-2. An XML Schema must reference the W3C Schema namespace, http://www.w3.org/2001/XMLSchema in its schema element. See the Add method for an example.

XmlSchemaCollection can be used by XmlValidatingReader for efficient data validation.

Example

[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
      rdr.Schemas.Add(sc)
      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

[C#] 
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.Schemas.Add(sc);
      rdr.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
      while (rdr.Read());
    }
    
  }

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

[C++] 
#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 {
public:
  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->Schemas->Add(sc);
      rdr->ValidationEventHandler += new ValidationEventHandler(0, ValidationCallBack);
      while (rdr->Read());
    }
    
  }

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

int main()
{
    ValidXSD::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.

Requirements

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

Show:
© 2014 Microsoft