This documentation is archived and is not being maintained.

XmlSchemaSet Class

Contains a cache of XML Schema definition language (XSD) schemas.

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

public class XmlSchemaSet

In System.Xml version 1.0, XML schemas were loaded into an XmlSchemaCollection class as a library of schemas. In System.Xml version 2.0, the XmlValidatingReader and the XmlSchemaCollection classes are obsolete, and have been replaced by the Create method and the XmlSchemaSet class, respectively.

The XmlSchemaSet has been introduced to fix a number of issues, including standards compatibility, performance, and the obsolete Microsoft XML-Data Reduced (XDR) schema format.

The following is a comparison between the XmlSchemaCollection class and the XmlSchemaSet class.



Supports Microsoft XDR and W3C XML schemas.

Only supports W3C XML schemas.

Schemas are compiled when the Add method is called.

Schemas are not compiled when the Add method is called. This provides a performance improvement during creation of the schema library.

Each schema generates an individual compiled version that can result in "schema islands." As a result, all includes and imports are scoped only within that schema.

Compiled schemas generate a single logical schema, a "set" of schemas. Any imported schemas within a schema that are added to the set are directly added to the set themselves. This means that all types are available to all schemas.

Only one schema for a particular target namespace can exist in the collection.

Multiple schemas for the same target namespace can be added as long as there are no type conflicts.

Security Considerations

  • External namespaces or locations referenced in include, import, and redefine elements of a schema are resolved with respect to the base URI of the schema that includes or imports them. For example, if the base URI of the including or importing schema is empty or a null reference (Nothing in Visual Basic), the external locations are resolved with respect to the current directory. The XmlUrlResolver class is used to resolve external schemas by default. To disable resolution of include, import, and redefine elements of a schema, set the XmlResolver property to a null reference (Nothing in Visual Basic).

  • The XmlSchemaSet class uses the System.Text.RegularExpressions.Regex class to parse and match regular expressions in an XML schema. Validation of pattern facets with regular expressions in an XML schema may involve increased CPU usage and should be avoided in high availability scenarios.

  • Exceptions raised as a result of using the XmlSchemaSet class, such as the XmlSchemaException class may contain sensitive information that should not be exposed in untrusted scenarios. For example, the SourceUri property of an XmlSchemaException returns the URI path to the schema file that caused the exception. The SourceUri property should not be exposed in untrusted scenarios. Exceptions should be properly handled so that this sensitive information is not exposed in untrusted scenarios.

The example below uses the XmlReaderSettings object and the XmlReader.Create method to associate a schema with an XML document. The schema is added to the Schemas property of the XmlReaderSettings object. The value of the Schemas property is an XmlSchemaSet object. The schema is used to validate that the XML document conforms to the schema content model. Schema validation errors and warnings are handled by the ValidationEventHandler defined in the XmlReaderSettings object.

No code example is currently available or this language may not be supported.
#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;
static void booksSettingsValidationEventHandler( Object^ /*sender*/, ValidationEventArgs^ e )
   if ( e->Severity == XmlSeverityType::Warning )
      Console::Write( L"WARNING: " );
      Console::WriteLine( e->Message );
   if ( e->Severity == XmlSeverityType::Error )
      Console::Write( L"ERROR: " );
      Console::WriteLine( e->Message );

int main()
   XmlReaderSettings * booksSettings = new XmlReaderSettings;
   booksSettings->Schemas->Add( L"", L"books.xsd" );
   booksSettings->XsdValidate = true;
   booksSettings->ValidationEventHandler += gcnew ValidationEventHandler( booksSettingsValidationEventHandler );
   XmlReader^ books = XmlReader::Create( L"books.xml", booksSettings );
   while ( books->Read() )

   return 0;

The example uses the books.xml file as input.

<bookstore xmlns="">
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>

The example uses the books.xsd file as an input.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="" xmlns:xs="">
    <xs:element name="bookstore">
                <xs:element maxOccurs="unbounded" name="book">
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="author">
                                        <xs:element minOccurs="0" name="name" type="xs:string" />
                                        <xs:element minOccurs="0" name="first-name" type="xs:string" />
                                        <xs:element minOccurs="0" name="last-name" type="xs:string" />
                            <xs:element name="price" type="xs:decimal" />
                        <xs:attribute name="genre" type="xs:string" use="required" />
                        <xs:attribute name="publicationdate" type="xs:unsignedShort" use="required" />
                        <xs:attribute name="ISBN" type="xs:string" use="required" />


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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0