XmlSchemaSet Class
Contains a cache of XML Schema definition language (XSD) schemas.
Assembly: System.Xml (in System.Xml.dll)
The XmlSchemaSet type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() | XmlSchemaSet | Initializes a new instance of the XmlSchemaSet class. |
![]() ![]() | XmlSchemaSet(XmlNameTable) | Initializes a new instance of the XmlSchemaSet class with the specified XmlNameTable. |
| Name | Description | |
|---|---|---|
![]() ![]() | CompilationSettings | Gets or sets the XmlSchemaCompilationSettings for the XmlSchemaSet. |
![]() ![]() | Count | Gets the number of logical XML Schema definition language (XSD) schemas in the XmlSchemaSet. |
![]() ![]() | GlobalAttributes | Gets all the global attributes in all the XML Schema definition language (XSD) schemas in the XmlSchemaSet. |
![]() ![]() | GlobalElements | Gets all the global elements in all the XML Schema definition language (XSD) schemas in the XmlSchemaSet. |
![]() ![]() | GlobalTypes | Gets all of the global simple and complex types in all the XML Schema definition language (XSD) schemas in the XmlSchemaSet. |
![]() ![]() | IsCompiled | Indicates if the XML Schema definition language (XSD) schemas in the XmlSchemaSet have been compiled. |
![]() ![]() | NameTable | Gets the default XmlNameTable used by the XmlSchemaSet when loading new XML Schema definition language (XSD) schemas. |
![]() ![]() | XmlResolver | Sets the XmlResolver used to resolve namespaces or locations referenced in include and import elements of a schema. |
| Name | Description | |
|---|---|---|
![]() ![]() | Add(XmlSchema) | Adds the given XmlSchema to the XmlSchemaSet. |
![]() ![]() | Add(XmlSchemaSet) | Adds all the XML Schema definition language (XSD) schemas in the given XmlSchemaSet to the XmlSchemaSet. |
![]() ![]() | Add(String, String) | Adds the XML Schema definition language (XSD) schema at the URL specified to the XmlSchemaSet. |
![]() ![]() | Add(String, XmlReader) | Adds the XML Schema definition language (XSD) schema contained in the XmlReader to the XmlSchemaSet. |
![]() ![]() | Compile | Compiles the XML Schema definition language (XSD) schemas added to the XmlSchemaSet into one logical schema. |
![]() ![]() | Contains(String) | Indicates whether an XML Schema definition language (XSD) schema with the specified target namespace URI is in the XmlSchemaSet. |
![]() ![]() | Contains(XmlSchema) | Indicates whether the specified XML Schema definition language (XSD) XmlSchema object is in the XmlSchemaSet. |
![]() ![]() | CopyTo | Copies all the XmlSchema objects from the XmlSchemaSet to the given array, starting at the given index. |
![]() ![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() ![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() ![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() ![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() ![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() ![]() | Remove | Removes the specified XML Schema definition language (XSD) schema from the XmlSchemaSet. |
![]() ![]() | RemoveRecursive | Removes the specified XML Schema definition language (XSD) schema and all the schemas it imports from the XmlSchemaSet. |
![]() ![]() | Reprocess | Reprocesses an XML Schema definition language (XSD) schema that already exists in the XmlSchemaSet. |
![]() ![]() | Schemas | Returns a collection of all the XML Schema definition language (XSD) schemas in the XmlSchemaSet. |
![]() ![]() | Schemas(String) | Returns a collection of all the XML Schema definition language (XSD) schemas in the XmlSchemaSet that belong to the given namespace. |
![]() ![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
| Name | Description | |
|---|---|---|
![]() ![]() | ValidationEventHandler | Sets an event handler for receiving information about XML Schema definition language (XSD) schema validation errors. |
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.
XmlSchemaCollection | XmlSchemaSet |
|---|---|
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 Nothing, 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 Nothing.
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.
Imports System Imports System.Xml Imports System.Xml.Schema Class XmlSchemaSetExample Shared Sub Main() Dim booksSettings As XmlReaderSettings = New XmlReaderSettings() booksSettings.Schemas.Add("http://www.contoso.com/books", "books.xsd") booksSettings.ValidationType = ValidationType.Schema AddHandler booksSettings.ValidationEventHandler, New ValidationEventHandler(AddressOf booksSettingsValidationEventHandler) Dim books As XmlReader = XmlReader.Create("books.xml", booksSettings) While books.Read() End While End Sub Shared Sub booksSettingsValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs) If e.Severity = XmlSeverityType.Warning Then Console.Write("WARNING: ") Console.WriteLine(e.Message) ElseIf e.Severity = XmlSeverityType.Error Then Console.Write("ERROR: ") Console.WriteLine(e.Message) End If End Sub End Class
The example uses the books.xml file as input.
<bookstore xmlns="http://www.contoso.com/books">
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
The example uses the books.xsd file as an input.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bookstore">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<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:sequence>
</xs:complexType>
</xs:element>
<xs:element name="price" type="xs:decimal" />
</xs:sequence>
<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" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.




