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 ref 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 FrameworkIsCompiledGets a value that indicates whether 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 the default hash function. (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 FrameworkValidationEventHandlerSpecifies an event handler for receiving information about XML Schema definition language (XSD) schema validation errors.
Top

Security noteSecurity Note
  • Do not use schemas from unknown or untrusted sources or locations. Doing so will compromise the security of your code.

  • XML schemas (including inline schemas) are inherently vulnerable to denial of service attacks; do not accept them in untrusted scenarios.

  • Schema validation error messages and exceptions may expose sensitive information about the content model or URI paths to the schema file. Be careful not to expose this information to untrusted callers.

  • Additional security considerations are covered in the "Security Considerations" section.

XmlSchemaSet is a cache or library where you can store XML Schema definition language (XSD) schemas. XmlSchemaSet improves performance by caching schemas in memory instead of accessing them from a file or a URL. Each schema is identified by the namespace URI and location that was specified when the schema was added to the set. You use the XmlReaderSettings::Schemas property to assign the XmlSchemaSet object an XML reader should use for data validation.

Security Considerations

  • Do not use schemas from unknown or untrusted sources. Doing so will compromise the security of your code. 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 nullptr, 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 XmlSchemaSet::XmlResolver property to nullptr.

  • 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 following example validates an XML file using schemas stored in the XmlSchemaSet. The namespace in the XML file, urn:bookstore-schema, identifies which schema in the XmlSchemaSet to use for validation.

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;
using namespace System::IO;

// Display any validation errors. 
static void ValidationCallBack( Object^ /*sender*/, ValidationEventArgs^ e )
{
   Console::WriteLine( L"Validation Error: {0}", e->Message );
}

int main()
{
   // Create the XmlSchemaSet class.
   XmlSchemaSet^ sc = gcnew XmlSchemaSet;

   // Add the schema to the collection.
   sc->Add( L"urn:bookstore-schema", L"books.xsd" );

   // Set the validation settings.
   XmlReaderSettings^ settings = gcnew XmlReaderSettings;
   settings->ValidationType = ValidationType::Schema;
   settings->Schemas = sc;
   settings->ValidationEventHandler += gcnew ValidationEventHandler( ValidationCallBack );

   // Create the XmlReader object.
   XmlReader^ reader = XmlReader::Create( L"booksSchemaFail.xml", settings );

   // Parse the file.  
   while ( reader->Read() )
      ;

   return 1;
}

Input

The sample uses the following two input files.

booksSchemaFail.xml:

<?xml version='1.0'?>
<bookstore xmlns="urn:bookstore-schema">
  <book>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
  </book>
  <book genre="novel">
    <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">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

books.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="urn:bookstore-schema"
    elementFormDefault="qualified"
    targetNamespace="urn:bookstore-schema">

 <xsd:element name="bookstore" type="bookstoreType"/>

 <xsd:complexType name="bookstoreType">
  <xsd:sequence maxOccurs="unbounded">
   <xsd:element name="book"  type="bookType"/>
  </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="bookType">
  <xsd:sequence>
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="author" type="authorName"/>
   <xsd:element name="price"  type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="genre" type="xsd:string"/>
 </xsd:complexType>

 <xsd:complexType name="authorName">
  <xsd:sequence>
   <xsd:element name="first-name"  type="xsd:string"/>
   <xsd:element name="last-name" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>

</xsd:schema>

Output

Validation Error: The element 'book' in namespace 'urn:bookstore-schema' has invalid child element 'author' in namespace 'urn:bookstore-schema'. Expected 'title' in namespace 'urn:bookstore-schema'.

Validation Error: The element 'author' in namespace 'urn:bookstore-schema' has invalid child element 'name' in namespace 'urn:bookstore-schema'. Expected 'first-name' in namespace 'urn:bookstore-schema'.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft