Export (0) Print
Expand All

XmlSchemaSet Class

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

System.Object
  System.Xml.Schema.XmlSchemaSet

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

public class XmlSchemaSet

The XmlSchemaSet type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkXmlSchemaSet()Initializes a new instance of the XmlSchemaSet class.
Public methodSupported by the XNA FrameworkXmlSchemaSet(XmlNameTable)Initializes a new instance of the XmlSchemaSet class with the specified XmlNameTable.
Top

  NameDescription
Public propertySupported by the XNA FrameworkCompilationSettingsGets or sets the XmlSchemaCompilationSettings for the XmlSchemaSet.
Public propertySupported by the XNA FrameworkCountGets the number of logical XML Schema definition language (XSD) schemas in the XmlSchemaSet.
Public propertySupported by the XNA FrameworkGlobalAttributesGets all the global attributes in all the XML Schema definition language (XSD) schemas in the XmlSchemaSet.
Public propertySupported by the XNA FrameworkGlobalElementsGets all the global elements in all the XML Schema definition language (XSD) schemas in the XmlSchemaSet.
Public propertySupported by the XNA FrameworkGlobalTypesGets all of the global simple and complex types in all the XML Schema definition language (XSD) schemas in the XmlSchemaSet.
Public propertySupported by the XNA FrameworkIsCompiledIndicates if the XML Schema definition language (XSD) schemas in the XmlSchemaSet have been compiled.
Public propertySupported by the XNA FrameworkNameTableGets the default XmlNameTable used by the XmlSchemaSet when loading new XML Schema definition language (XSD) schemas.
Public propertySupported by the XNA FrameworkXmlResolverSets the XmlResolver used to resolve namespaces or locations referenced in include and import elements of a schema.
Top

  NameDescription
Public methodSupported by the XNA FrameworkAdd(XmlSchema)Adds the given XmlSchema to the XmlSchemaSet.
Public methodSupported by the XNA FrameworkAdd(XmlSchemaSet)Adds all the XML Schema definition language (XSD) schemas in the given XmlSchemaSet to the XmlSchemaSet.
Public methodSupported by the XNA FrameworkAdd(String, String)Adds the XML Schema definition language (XSD) schema at the URL specified to the XmlSchemaSet.
Public methodSupported by the XNA FrameworkAdd(String, XmlReader)Adds the XML Schema definition language (XSD) schema contained in the XmlReader to the XmlSchemaSet.
Public methodSupported by the XNA FrameworkCompileCompiles the XML Schema definition language (XSD) schemas added to the XmlSchemaSet into one logical schema.
Public methodSupported by the XNA FrameworkContains(String)Indicates whether an XML Schema definition language (XSD) schema with the specified target namespace URI is in the XmlSchemaSet.
Public methodSupported by the XNA FrameworkContains(XmlSchema)Indicates whether the specified XML Schema definition language (XSD) XmlSchema object is in the XmlSchemaSet.
Public methodSupported by the XNA FrameworkCopyToCopies all the XmlSchema objects from the XmlSchemaSet to the given array, starting at the given index.
Public methodSupported by the XNA FrameworkEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkRemoveRemoves the specified XML Schema definition language (XSD) schema from the XmlSchemaSet.
Public methodSupported by the XNA FrameworkRemoveRecursiveRemoves the specified XML Schema definition language (XSD) schema and all the schemas it imports from the XmlSchemaSet.
Public methodSupported by the XNA FrameworkReprocessReprocesses an XML Schema definition language (XSD) schema that already exists in the XmlSchemaSet.
Public methodSupported by the XNA FrameworkSchemas()Returns a collection of all the XML Schema definition language (XSD) schemas in the XmlSchemaSet.
Public methodSupported by the XNA FrameworkSchemas(String)Returns a collection of all the XML Schema definition language (XSD) schemas in the XmlSchemaSet that belong to the given namespace.
Public methodSupported by the XNA FrameworkToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public eventSupported by the XNA FrameworkValidationEventHandlerSets an event handler for receiving information about XML Schema definition language (XSD) schema validation errors.
Top

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 null, 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 null.

  • 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.


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

class XmlSchemaSetExample
{
    static void Main()
    {
        XmlReaderSettings booksSettings = new XmlReaderSettings();
        booksSettings.Schemas.Add("http://www.contoso.com/books", "books.xsd");
        booksSettings.ValidationType = ValidationType.Schema;
        booksSettings.ValidationEventHandler += new ValidationEventHandler(booksSettingsValidationEventHandler);

        XmlReader books = XmlReader.Create("books.xml", booksSettings);

        while (books.Read()) { }
    }

    static void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e)
    {
        if (e.Severity == XmlSeverityType.Warning)
        {
            Console.Write("WARNING: ");
            Console.WriteLine(e.Message);
        }
        else if (e.Severity == XmlSeverityType.Error)
        {
            Console.Write("ERROR: ");
            Console.WriteLine(e.Message);
        }
    }
}


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>


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

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

Community Additions

ADD
Show:
© 2014 Microsoft