System.Xml 2.0.0.0 4.0.0.0 System.Object

In version 1.0, XML schemas were loaded into an class as a library of schemas. In version 2.0, the and the classes are obsolete, and have been replaced by the method and the class, respectively.

The 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 class and the class.

XmlSchemaCollection

XmlSchemaSet

Supports Microsoft XDR and W3C XML schemas.

Only supports W3C XML schemas.

Schemas are compiled when the method is called.

Schemas are not compiled when the 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 class is used to resolve external schemas by default. To disable resolution of include, import, and redefine elements of a schema, set the property to null.

The class uses the 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 class, such as the class may contain sensitive information that should not be exposed in untrusted scenarios. For example, the property of an returns the URI path to the schema file that caused the exception. The property should not be exposed in untrusted scenarios. Exceptions should be properly handled so that this sensitive information is not exposed in untrusted scenarios.

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

- Constructor
  2.0.0.0
  4.0.0.0
  To be added.

  Initializes a new instance of the class.

- Constructor
  2.0.0.0
  4.0.0.0
  To be added.

  Initializes a new instance of the class with the specified .

  The object to use.
- Method
  2.0.0.0
  4.0.0.0
  System.Xml.Schema.XmlSchema ``

  If the object already exists in the , the method does nothing.

  The functionality of this method is identical to that of the method.

  Adds the given to the .

  An object if the schema is valid. If the schema is not valid and a is specified, then null is returned and the appropriate validation event is raised. Otherwise, an is thrown.

  The object to add to the .
- Method
  2.0.0.0
  4.0.0.0
  System.Void ``

  Before a schema can be added to an , it has to be successfully preprocessed. Preprocessing performs the following basic tasks.

  The schema is checked for structural validity according to the rules of W3C XML Schema, but the schema is not fully validated.

  References to internal and external schema components are resolved. Any imported or included schemas that are successfully retrieved are also added to the . Imported schemas are added as separate objects, and included schemas are made a part of the including .

  If the property of the to add is true, all schemas in the to add are added to the . If the property of the to add is false, each schema added is preprocessed before being added. If any of the schemas in the newly added fails to be preprocessed, no schemas are added; instead, an is thrown. As a result, the following two code example are not equivalent.

  `' First example schemaSet.Add(schemaSet1) ' Second example Dim schema As XmlSchema For Each schema in schemaSet.Schemas() schemaSet.Add(schema) Next` `// First example schemaSet.Add(schemaSet1); // Second example foreach(XmlSchema schema in schemaSet.Schemas()) { schemaSet.Add(schema); }`

  The previous two code examples are not equivalent. In the first example, if an invalid schema exists in schemaSet1 and its property is set to false, no schemas are added to schemaSet. In the second example, a number of schemas can be added to schemaSet before an invalid schema is encountered and an exception is thrown.

  Adds all the XML Schema definition language (XSD) schemas in the given to the .

  The object.
- Method
  2.0.0.0
  4.0.0.0
  System.Xml.Schema.XmlSchema `` ``

  Before a schema can be added to an , it has to be successfully preprocessed. Preprocessing performs the following basic tasks.

  The schema is checked for structural validity according to the rules of W3C XML Schema, but the schema is not fully validated.

  References to internal and external schema components are resolved. Any imported or included schemas that are successfully retrieved are also added to the . Imported schemas are added as separate objects, and included schemas are made a part of the including .

  The following are important notes to consider when using the method.

  Adding a schema to the with the same target namespace and schema location URL as a schema already contained within the will return the original schema object.

  When a new schema is successfully added to an , the property of the is set to false.

  Any include or import elements encountered in an XML schema are resolved when the method is called. Failure to resolve include and import elements results in a schema validation warning and if no has been specified for the object, these warning will not be reported.

  If a schema with the same target namespace as a schema that already exists in the is added to the , both schemas are added.

  This behavior differs from the obsolete object.

  The method of the has the ability to use the target namespace defined in a schema, rather than requiring the target namespace be specified as a parameter when the method is called. Specifying null in the parameter of the method instructs the to use the target namespace defined in the schema, as illustrated in the following code example.

  `Dim schemaSet As XmlSchemaSet = New XmlSchemaSet() schemaSet.Add(Nothing, "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(null, "books.xsd"); foreach(XmlSchema schema in schemaSet.Schemas("http://www.contoso.com/books")) { schema.Write(Console.Out); }`

  In the code example above, null is specified as the parameter to the method. As a result, the targetNamespace defined in the books.xml file is used. In this case, the result of calling the method would be identical if http://www.contoso.com/books had been specified as the parameter.

  W3C XML Schema allows schemas without a target namespace to be included in schemas with a target namespace defined. In this case, the schema without a target namespace defined is coerced into the target namespace of the including schema. The included schema is treated as if it had that target namespace defined. Similarly, schemas without a target namespace can be added to the and coerced into the target namespace specified by the method, as illustrated in the following example.

  `<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="A" type="xs:string" /> </xs:schema>`

  If the schema above is added to the with the target namespace http://www.contoso.com/new/targetnamespace (as shown in the code below), it is treated as if the target namespace declared in the schema was http://www.contoso.com/new/targetnamespace.

  `Dim schemaSet As XmlSchemaSet = New XmlSchemaSet() schemaSet.Add("http://www.contoso.com/new/targetnamespace", "http://www.contoso.com/targetnamespace.xsd") 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/new/targetnamespace", "http://www.contoso.com/targetnamespace.xsd"); foreach(XmlSchema schema in schemaSet.Schemas()) { Console.WriteLine(schema.TargetNamespace); }`

  Adds the XML Schema definition language (XSD) schema at the URL specified to the .

  An object if the schema is valid. If the schema is not valid and a is specified, then null is returned and the appropriate validation event is raised. Otherwise, an is thrown.

  The schema targetNamespace property, or null to use the targetNamespace specified in the schema. The URL that specifies the schema to load.
- Method
  2.0.0.0
  4.0.0.0
  System.Xml.Schema.XmlSchema `` ``

  Before a schema can be added to an , it has to be successfully preprocessed. Preprocessing performs the following basic tasks.

  The schema is checked for structural validity according to the rules of W3C XML Schema, but the schema is not fully validated.

  References to internal and external schema components are resolved. Any imported or included schemas that are successfully retrieved are also added to the . Imported schemas are added as separate objects, and included schemas are made a part of the including .

  The following are important notes to consider when using the method.

  Successfully retrieved schemas imported or included by the schemas contained in the are also added to the .

  If the is not positioned on the root element, an is thrown unless the current item is an element. If the current item is an xs:schema element, the schema document is read into the ; otherwise, an is thrown because the schema is not valid.

  If the is positioned over a sequence of XML nodes, only the first node in the sequence is added.

  If the schema was created from a method call, the value of the property is ignored, because inline schema processing is not applied for W3C XML Schema documents.

  The property of the is not used to resolve references to namespaces or schema locations in include and import elements. Instead, the property of the is used.

  The method of the has the ability to use the target namespace defined in a schema, rather than requiring the target namespace be specified as a parameter when the method is called. Specifying null or to the method instructs the to use the target namespace defined in the schema. For an example of this behavior, see the method.

  The remaining functionality of this method is identical to that of the method.

  Adds the XML Schema definition language (XSD) schema contained in the to the .

  An object if the schema is valid. If the schema is not valid and a is specified, then null is returned and the appropriate validation event is raised. Otherwise, an is thrown.

  The schema targetNamespace property, or null to use the targetNamespace specified in the schema. The object.
- Property
  2.0.0.0
  4.0.0.0
  System.Xml.Schema.XmlSchemaCompilationSettings To be added.

  The class specifies Unique Particle Attribution (UPA) validation options for the . For more information about enabling UPA validation checks, see the property.

  Gets or sets the for the .

- Method
  2.0.0.0
  4.0.0.0
  System.Void

  This method is called automatically when validation is needed and the has not been previously compiled—for example, when an is used as an input to create an object in the property of an object. If the is already in the compiled state, this method will not recompile the schemas. If this method executes successfully, the property is set to true.

  Schemas that have been previously compiled by an are not recompiled. However, schemas that were compiled using the method of the and the of the will be recompiled.

  You need to call the method if you have changed a schema (or one of its includes/imports) after adding it to the . The method will check the schema for structural validity according to the rules of W3C XML Schema. However, it will not perform a full validation check. It will also resolve references to internal and external schema components. Any imported or included schemas that are successfully retrieved are also added to the . Imported schemas are added as separate objects while included schemas are made part of the including . If the call to reprocess is successful, the property is set to false.

  Compiles the XML Schema definition language (XSD) schemas added to the into one logical schema.

- Method
  2.0.0.0
  4.0.0.0
  System.Boolean ``

  Schemas that are indirectly added to the are detected by the method; for example, imported schemas. As a result, if a schema for the http://www.contoso.com/retail namespace which imports a schema for the http://www.contoso.com/books namespace is added to the , calling with as a parameter, it returns true.

  Indicates whether an XML Schema definition language (XSD) schema with the specified target namespace URI is in the .

  true if a schema with the specified target namespace URI is in the ; otherwise, false.

  The schema targetNamespace property.
- Method
  2.0.0.0
  4.0.0.0
  System.Boolean `` To be added.

  Indicates whether the specified XML Schema definition language (XSD) object is in the .

  true if the object is in the ; otherwise, false.

  The object.
- Method
  2.0.0.0
  4.0.0.0
  System.Void `` `` To be added.

  Copies all the objects from the to the given array, starting at the given index.

  The array to copy the objects to. The index in the array where copying will begin.
- Property
  2.0.0.0
  4.0.0.0
  System.Int32 To be added.

  If there are two schemas for the namespace http://www.contoso.com in the , the property would return 1 because the schemas are treated as a single logical schema for validation purposes. However, if a schema for the namespace http://www.contoso.com/retail imported a schema for the http://www.contoso.com/books namespace, the value of the property would be 2.

  Gets the number of logical XML Schema definition language (XSD) schemas in the .

- Property
  2.0.0.0
  4.0.0.0
  System.Xml.Schema.XmlSchemaObjectTable To be added. To be added.

  Gets all the global attributes in all the XML Schema definition language (XSD) schemas in the .

- Property
  2.0.0.0
  4.0.0.0
  System.Xml.Schema.XmlSchemaObjectTable To be added. To be added.

  Gets all the global elements in all the XML Schema definition language (XSD) schemas in the .

- Property
  2.0.0.0
  4.0.0.0
  System.Xml.Schema.XmlSchemaObjectTable To be added.

  The property always returns a type that represents the xs:anyType complex type.

  Gets all of the global simple and complex types in all the XML Schema definition language (XSD) schemas in the .

- Property
  2.0.0.0
  4.0.0.0
  System.Boolean To be added.

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

  Gets a value that indicates whether the XML Schema definition language (XSD) schemas in the have been compiled.

- Property
  2.0.0.0
  4.0.0.0
  System.Xml.XmlNameTable To be added. To be added.

  Gets the default used by the when loading new XML Schema definition language (XSD) schemas.

- Method
  2.0.0.0
  4.0.0.0
  System.Xml.Schema.XmlSchema ``

  Removing a schema from the sets the property to false.

  Removes the specified XML Schema definition language (XSD) schema from the .

  The object removed from the or null if the schema was not found in the .

  The object to remove from the .
- Method
  2.0.0.0
  4.0.0.0
  System.Boolean ``

  The method removes the specified schema and all the schemas it imports from the , as long as there are no dependencies on the schema or its imported schemas. If there are dependencies on the schema or its imported schemas in the , nothing is removed and returns false. If false is returned and a is defined, a warning is sent to the event handler describing the dependencies.

  If the specified schema imports other schemas and the specified schema was previously removed with the method, the method will not remove the imported schemas and will return false. For example, if imports and the following code will only remove , but not the imported and schemas:

  `XmlSchemaSet ss = new XmlSchemaSet(); XmlSchema xs = XmlSchema.Read(XmlReader.Create("parentSchema.xsd"), null); ss.Add(xs); ss.Compile(); ss.Remove(xs); ss.Compile(); ss.RemoveRecursive(xs); ss.Compile();`

  The following code will remove the and the imported schemas:

  `XmlSchemaSet ss = new XmlSchemaSet(); XmlSchema xs = XmlSchema.Read(XmlReader.Create("parentSchema.xsd"), null); ss.Add(xs); ss.Compile(); ss.RemoveRecursive(xs); ss.Compile();`

  The method has no effect on the state of the property.

  Removes the specified XML Schema definition language (XSD) schema and all the schemas it imports from the .

  true if the object and all its imports were successfully removed; otherwise, false.

  The object to remove from the .
- Method
  2.0.0.0
  4.0.0.0
  System.Xml.Schema.XmlSchema ``

  Reprocessing a schema performs all the preprocessing steps performed on a schema when the method is called. If the call to is successful, the property is set to false.

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

  You need to call the method if you have changed a schema (or one of its includes/imports) after adding it to the . The method will check the schema for structural validity according to the rules of W3C XML Schema. However, it will not perform a full validation check. It will also resolve references to internal and external schema components. Any imported or included schemas that are successfully retrieved are also added to the . Imported schemas are added as separate objects while included schemas are made part of the including . If the call to reprocess is successful, the property is set to false.

  Reprocesses an XML Schema definition language (XSD) schema that already exists in the .

  An object if the schema is a valid schema. If the schema is not valid and a is specified, null is returned and the appropriate validation event is raised. Otherwise, an is thrown.

  The schema to reprocess.
- Method
  2.0.0.0
  4.0.0.0
  System.Collections.ICollection

  This method returns schemas that were added indirectly to the because they were imported.

  The method is the equivalent of the method of the obsolete .

  Returns a collection of all the XML Schema definition language (XSD) schemas in the .

  An object containing all the schemas that have been added to the . If no schemas have been added to the , an empty object is returned.

- Method
  2.0.0.0
  4.0.0.0
  System.Collections.ICollection ``

  If the parameter is null or , then all schemas without a namespace are returned.

  This method returns schemas that were added indirectly to the because they were imported.

  The method is the equivalent of the method of the obsolete .

  Returns a collection of all the XML Schema definition language (XSD) schemas in the that belong to the given namespace.

  An object containing all the schemas that have been added to the that belong to the given namespace. If no schemas have been added to the , an empty object is returned.

  The schema targetNamespace property.
- Event
  2.0.0.0
  4.0.0.0
  System.Xml.Schema.ValidationEventHandler

  Sets an event handler for receiving information about schema validation errors when the or methods of the are called.

  If an event handler is not defined, an is thrown on any validation errors where the is . Exceptions are not thrown for validation errors with an of .

  Specifies an event handler for receiving information about XML Schema definition language (XSD) schema validation errors.

- Property
  2.0.0.0
  4.0.0.0
  System.Xml.XmlResolver To be added.

  The of an is used to resolve namespaces or locations referenced in include and import elements of a schema, any time a schema is added using the or methods.

  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 class is used to resolve external schemas by default. To disable resolution of include, import, and redefine elements of a schema, set the property to null.

  Sets the used to resolve namespaces or locations referenced in include and import elements of a schema.