System.Xml.Schema.XmlSchemaSet.Add Method

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

Syntax

public XmlSchema Add (string targetNamespace, string schemaUri)

Parameters

targetNamespace
The schema targetNamespace property, or null to use the targetNamespace specified in the schema.
schemaUri
The URL that specifies the schema to load.

Returns

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

Remarks

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

[The 'ordered' type of list has not been implemented in the ECMA stylesheet.]

The following are important notes to consider when using the XmlSchemaSet.Add(string, string) method.

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

Example

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 targetNamespace parameter to the XmlSchemaSet.Add(string, string) method. As a result, the targetNamespace defined in the books.xml file is used. In this case, the result of calling the XmlSchemaSet.Add(string, string) method would be identical if http://www.contoso.com/books had been specified as the targetNamespace 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 System.Xml.Schema.XmlSchemaSet and coerced into the target namespace specified by the XmlSchemaSet.Add(string, string) method, as illustrated in the following example.

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 System.Xml.Schema.XmlSchemaSet 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.

Example

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

Example

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);
}

Requirements

Namespace: System.Xml.Schema
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0