Decimal places of the XML schema when multiplying

"XmlSchemaSet" for compiling schemes

  • 3 minutes to read

Describes the XmlSchemaSet, which is a cache in which XSD schemas (XML Schema Definition Language) can be stored and validated.

The XmlSchemaSet class

The XmlSchemaSet is a cache in which XSD schemas (XML Schema Definition Language) can be stored and validated.

In System.Xml version 1.0, XML Schemas were loaded into an XmlSchemaCollection class as a schema library. In System.Xml version 2.0, the XmlValidatingReader and XmlSchemaCollection classes are deprecated and have been replaced by the Create methods and the XmlSchemaSet class, respectively.

The XmlSchemaSet was introduced to address a number of issues including standards compatibility, performance, and the outdated Microsoft XDR (XML-Data Reduced) schema format.

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

XmlSchemaCollectionXmlSchemaSet
Supports Microsoft XDR Schemas and XML Schemas from the W3C.Only supports XML schemas from the W3C.
The schemas are compiled when the Add method is called.The schemas are not compiled when the Add method is called. This increases the efficiency during the creation of the schema library.
A single compiled version of each schema is created, which can lead to "islands of schema". As a result, all includes and imports are only assigned within this schema.Compiled schemes create a single logical scheme, a "group" of schemes. All imported schemes within a scheme that are added to the group are added directly to the group itself. Therefore, all types are available for all schemes.
There can only be one schema for a particular target namespace in the collection.Multiple schemas can be added for the same target namespace as long as there are no type conflicts.

Migrate to the "XmlSchemaSet"

The following code example provides instructions on how to migrate from the deprecated XmlSchemaCollection class to the new XmlSchemaSet class. The code example demonstrates the following key differences between the two classes.

The following is an example of the deprecated XmlSchemaCollection code.

Below is an example of the corresponding XmlSchemaSet code.

Adding and retrieving schemes

Schemas are added to an XmlSchemaSet using the Add method of the XmlSchemaSet. When a scheme is added to an XmlSchemaSet, it is mapped to a namespace URI. The target namespace URI can be specified as a parameter for the Add method. If no namespace is specified, the XmlSchemaSet uses the target namespace specified in the schema.

Schemas are obtained from the XmlSchemaSet using the XmlSchemaSet property of the schema. The Schemas property of the XmlSchemaSet allows you to iterate through the XmlSchemaSet objects contained in the XmlSchema. The Schemas property either returns all XmlSchemaSet objects contained in the XmlSchema. Otherwise, if a parameter was specified for the target namespace, all XmlSchema objects belonging to this target namespace are returned. When specified as a parameter for the target namespace, the Schemas property returns all schemas with no namespace.

The following example adds the schema in the XmlSchemaSet namespace to one. In addition, all schemas belonging to the namespace are retrieved from the XmlSchemaSet and written to the console.

For more information about adding and getting Schemas from an XmlSchemaSet object, see the reference documentation for the Add Method and the Schemas Property.

Compiling Schemas

Schemas in an XmlSchemaSet are compiled into a logical scheme by the Compile method of the XmlSchemaSet.

Note

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

If the Compile method succeeds, the IsCompiled property of the XmlSchemaSet is set to.

Note

The IsCompiled property is not affected when schemas are edited in the XmlSchemaSet. Updates to the individual schemas in the XmlSchemaSet are not tracked. As a result, the IsCompiled property can be even though one of the schemas contained in the XmlSchemaSet is out of date if no schemas have been added or removed from the XmlSchemaSet.

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

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

Scheme reprocessing

When reprocessing a schema in an XmlSchemaSet, all reprocessing steps are performed that are performed when calling the Add method of the XmlSchemaSet on a schema. If the Reprocess method was called successfully, the IsCompiled property of the XmlSchemaSet is set to.

The Reprocess method should be used when a schema in the XmlSchemaSet has been changed after the XmlSchemaSet compiled.

The following example shows reprocessing a schema that was 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 changed, the IsCompiled property is set to even though a schema in the XmlSchemaSet has changed. When the Reprocess method is called, any reprocessing steps performed by the Add method are performed and the IsCompiled property is set to.

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

Look for a scheme

You can use the Contains method of the XmlSchemaSet to determine whether an XmlSchemaSet contains a schema. The Contains method looks for either a target namespace or an XmlSchema object. In both cases, the Contains method returns if there is a schema in the XmlSchemaSet. Otherwise, it returns.

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

Removing Schemes

Schemas are removed from an XmlSchemaSet using the XmlSchemaSet method and the Remove method of RemoveRecursive. The Remove method removes the specified scheme from the XmlSchemaSet, whereas the RemoveRecursive method removes the specified scheme and all schemes imported from the XmlSchemaSet.

The following example shows how to add multiple schemes to an XmlSchemaSet and then use the RemoveRecursive method to remove one of the schemes and all of the schemes it imports.

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

Schema resolution and "xs: import"

The following examples describe how the XmlSchemaSet behaves when importing schemas when there are multiple schemas for a given namespace in an XmlSchemaSet.

For example, consider an XmlSchemaSet that contains multiple schemas for the namespace. A schema with the following statement is added to the XmlSchemaSet.

The XmlSchemaSet tries to import a schema for the namespace by loading it from the URL. In the import schema, only the schema declaration and the types declared in the schema document are available, although there are other schema documents for the XmlSchemaSet namespace in the other. If the file cannot be found at the URL, no scheme for the namespace is imported into the import scheme.

Validation of XML documents

XML documents can be validated using 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 class 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 to validate the XML document.

For more information about validating XML documents using an XmlSchemaSet class, see Validating an XML Schema (XSD) Using XmlSchemaSet.

See also