Uređivanje

Dijeli putem


XmlSchemaSet for Schema Compilation

Describes the XmlSchemaSet, a cache where XML Schema definition language (XSD) schemas can be stored and validated.

The XmlSchemaSet Class

The XmlSchemaSet is a cache where XML Schema definition language (XSD) schemas can be stored and validated.

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

Migrating to the XmlSchemaSet

The following code example provides a guide to migrating to the new XmlSchemaSet class from the obsolete XmlSchemaCollection class. The code example illustrates the following major differences between the two classes.

The following is the obsolete XmlSchemaCollection code example.

Dim schemaCollection As XmlSchemaCollection = New XmlSchemaCollection()  
schemaCollection.Add("http://www.contoso.com/retail", "http://www.contoso.com/retail.xsd")  
schemaCollection.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd")  
  
Dim schema As XmlSchema  
  
For Each schema in schemaCollection  
  
   Console.WriteLine(schema.TargetNamespace)  
  
Next  
XmlSchemaCollection schemaCollection = new XmlSchemaCollection();  
schemaCollection.Add("http://www.contoso.com/retail", "http://www.contoso.com/retail.xsd");  
schemaCollection.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd");  
  
foreach(XmlSchema schema in schemaCollection)  
{  
   Console.WriteLine(schema.TargetNamespace);  
}  

The following is the equivalent XmlSchemaSet code example.

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()  
schemaSet.Add("http://www.contoso.com/retail", "http://www.contoso.com/retail.xsd")  
schemaSet.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd")  
schemaSet.Compile()  
  
Dim schema As XmlSchema  
  
For Each schema in schemaSet.Schemas()  
  
   Console.WriteLine(schema.TargetNamespace)  
  
Next  
XmlSchemaSet schemaSet = new XmlSchemaSet();  
schemaSet.Add("http://www.contoso.com/retail", "http://www.contoso.com/retail.xsd");  
schemaSet.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd");  
schemaSet.Compile();  
  
foreach(XmlSchema schema in schemaSet.Schemas())  
{  
   Console.WriteLine(schema.TargetNamespace);  
}  

Adding and Retrieving Schemas

Schemas are added to an XmlSchemaSet using the Add method of the XmlSchemaSet. When a schema is added to an XmlSchemaSet, it is associated with a target namespace URI. The target namespace URI can either be specified as a parameter to the Add method or if no target namespace is specified, the XmlSchemaSet uses the target namespace defined in the schema.

Schemas are retrieved from an XmlSchemaSet using the Schemas property of the XmlSchemaSet. The Schemas property of the XmlSchemaSet allows you to iterate over the XmlSchema objects contained in the XmlSchemaSet. The Schemas property either returns all the XmlSchema objects contained in the XmlSchemaSet, or, given a target namespace parameter, returns all the XmlSchema objects that belong to the target namespace. If null is specified as the target namespace parameter, the Schemas property returns all schemas without a namespace.

The following example adds the books.xsd schema in the http://www.contoso.com/books namespace to an XmlSchemaSet, retrieves all schemas that belong to the http://www.contoso.com/books namespace from the XmlSchemaSet, and then writes those schemas to the Console.

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet  
schemaSet.Add("http://www.contoso.com/books", "books.xsd")  
  
Dim schema As XmlSchema  
  
For Each schema In schemaSet.Schemas("http://www.contoso.com/books")  
  
   schema.Write(Console.Out)  
  
Next  
XmlSchemaSet schemaSet = new XmlSchemaSet();  
schemaSet.Add("http://www.contoso.com/books", "books.xsd");  
  
foreach (XmlSchema schema in schemaSet.Schemas("http://www.contoso.com/books"))  
{  
   schema.Write(Console.Out);  
}  

For more information about adding and retrieving schemas from an XmlSchemaSet object, see the Add method and the Schemas property reference documentation.

Compiling Schemas

Schemas in an XmlSchemaSet are compiled into one logical schema by the Compile method of the XmlSchemaSet.

Note

Unlike the obsolete XmlSchemaCollection class, schemas are not compiled when the Add method is called.

If the Compile method executes successfully, the IsCompiled property of the XmlSchemaSet is set to true.

Note

The IsCompiled property is not affected if schemas are edited while in the XmlSchemaSet. Updates of the individual schemas in the XmlSchemaSet are not tracked. As a result, the IsCompiled property can be true even though one of the schemas contained in the XmlSchemaSet has been altered, as long as no schemas were added or removed from the XmlSchemaSet.

The following example adds the books.xsd file to the XmlSchemaSet and then calls the Compile method.

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()  
schemaSet.Add("http://www.contoso.com/books", "books.xsd")  
schemaSet.Compile()  
XmlSchemaSet schemaSet = new XmlSchemaSet();  
schemaSet.Add("http://www.contoso.com/books", "books.xsd");  
schemaSet.Compile();  

For more information about compiling schemas in an XmlSchemaSet, see the Compile method reference documentation.

Reprocessing Schemas

Reprocessing a schema in an XmlSchemaSet performs all the preprocessing steps performed on a schema when the Add method of the XmlSchemaSet is called. If the call to the Reprocess method is successful, the IsCompiled property of the XmlSchemaSet is set to false.

The Reprocess method should be used when a schema in the XmlSchemaSet has been modified after the XmlSchemaSet has performed compilation.

The following example illustrates reprocessing a schema added to the XmlSchemaSet using the Reprocess method. After the XmlSchemaSet is compiled using the Compile method, and the schema added to the XmlSchemaSet is modified, the IsCompiled property is set to true even though a schema in the XmlSchemaSet has been modified. Calling the Reprocess method performs all the preprocessing performed by the Add method, and sets the IsCompiled property to false.

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()  
Dim schema As XmlSchema = schemaSet.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd")  
schemaSet.Compile()  
  
Dim element As XmlSchemaElement = New XmlSchemaElement()  
schema.Items.Add(element)  
element.Name = "book"  
element.SchemaTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")  
  
schemaSet.Reprocess(schema)  
XmlSchemaSet schemaSet = new XmlSchemaSet();  
XmlSchema schema = schemaSet.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd");  
schemaSet.Compile();  
  
XmlSchemaElement element = new XmlSchemaElement();  
schema.Items.Add(element);  
element.Name = "book";  
element.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");  
  
schemaSet.Reprocess(schema);  

For more information about reprocessing a schema in an XmlSchemaSet, see the Reprocess method reference documentation.

Checking for a Schema

You can use the Contains method of the XmlSchemaSet to check if a schema is contained within an XmlSchemaSet. The Contains method takes either a target namespace or an XmlSchema object to check for. In either case, the Contains method returns true if the schema is contained within the XmlSchemaSet; otherwise, it returns false.

For more information about checking for a schema, see the Contains method reference documentation.

Removing Schemas

Schemas are removed from an XmlSchemaSet using the Remove and RemoveRecursive methods of the XmlSchemaSet. The Remove method removes the specified schema from the XmlSchemaSet, while the RemoveRecursive method removes the specified schema and all the schemas it imports from the XmlSchemaSet.

The following example illustrates adding multiple schemas to an XmlSchemaSet, then using the RemoveRecursive method to remove one of the schemas and all the schemas it imports.

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()  
schemaSet.Add("http://www.contoso.com/retail", "http://www.contoso.com/retail.xsd")  
schemaSet.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd")  
schemaSet.Add("http://www.contoso.com/music", "http://www.contoso.com/music.xsd")  
  
Dim schema As XmlSchema  
  
For Each schema In schemaSet.Schemas()  
  
   If schema.TargetNamespace = "http://www.contoso.com/music" Then  
      schemaSet.RemoveRecursive(schema)  
   End If  
  
Next  
XmlSchemaSet schemaSet = new XmlSchemaSet();  
schemaSet.Add("http://www.contoso.com/retail", "http://www.contoso.com/retail.xsd");  
schemaSet.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd");  
schemaSet.Add("http://www.contoso.com/music", "http://www.contoso.com/music.xsd");  
  
foreach (XmlSchema schema in schemaSet.Schemas())  
{  
   if (schema.TargetNamespace == "http://www.contoso.com/music")  
   {  
      schemaSet.RemoveRecursive(schema);  
   }  
}  

For more information about removing schemas from an XmlSchemaSet, see the Remove and RemoveRecursive methods reference documentation.

Schema Resolution and xs:import

The following examples describe the XmlSchemaSet behavior for importing schemas when multiple schemas for a given namespace exist in an XmlSchemaSet.

For example, consider an XmlSchemaSet that contains multiple schemas for the http://www.contoso.com namespace. A schema with the following xs:import directive is added to the XmlSchemaSet.

<xs:import namespace="http://www.contoso.com" schemaLocation="http://www.contoso.com/schema.xsd" />  

The XmlSchemaSet attempts to import a schema for the http://www.contoso.com namespace by loading it from the http://www.contoso.com/schema.xsd URL. Only the schema declaration and types declared in the schema document are available in the importing schema, even though there are other schema documents for the http://www.contoso.com namespace in the XmlSchemaSet. If the schema.xsd file cannot be located at the http://www.contoso.com/schema.xsd URL, no schema for the http://www.contoso.com namespace is imported into the importing schema.

Validating XML Documents

XML documents can be validated against schemas in an XmlSchemaSet. You validate an XML document by adding a schema to the XmlSchemaSetSchemas property of an XmlReaderSettings object, or by adding an XmlSchemaSet to the Schemas property of an XmlReaderSettings object. The XmlReaderSettings object is then used by the Create method of the XmlReader class to create an XmlReader object and validate the XML document.

For more information about validating XML documents using an XmlSchemaSet, see XML Schema (XSD) Validation with XmlSchemaSet.

See also