Export (0) Print
Expand All

XmlSchemaSet.Add Method (String, String)

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

Namespace:  System.Xml.Schema
Assembly:  System.Xml (in System.Xml.dll)

public XmlSchema Add(
	string targetNamespace,
	string schemaUri
)

Parameters

targetNamespace
Type: System.String

The schema targetNamespace property, or null to use the targetNamespace specified in the schema.

schemaUri
Type: System.String

The URL that specifies the schema to load.

Return Value

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

ExceptionCondition
XmlSchemaException

The schema is not valid.

ArgumentNullException

The URL passed as a parameter is null or Empty.

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

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

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

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

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

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

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

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

    NoteNote

    This behavior differs from the obsolete XmlSchemaCollection object.

  • The Add method of the XmlSchemaSet 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 Add method is called. Specifying null in the targetNamespace parameter of the Add method instructs the XmlSchemaSet to use the target namespace defined in the schema, as illustrated in the following code 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 Add method. As a result, the targetNamespace defined in the books.xml file is used. In this case, the result of calling the Add 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 XmlSchemaSet and coerced into the target namespace specified by the Add 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 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.

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

The following code example adds the http://www.contoso.com/books.xsd schema with a target namespace of http://www.contoso.com/books to the XmlSchemaSet.

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

The example uses the books.xsd file as input.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="bookstore">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="book">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="author">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element minOccurs="0" name="name" type="xs:string" />
                                        <xs:element minOccurs="0" name="first-name" type="xs:string" />
                                        <xs:element minOccurs="0" name="last-name" type="xs:string" />
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="price" type="xs:decimal" />
                        </xs:sequence>
                        <xs:attribute name="genre" type="xs:string" use="required" />
                        <xs:attribute name="publicationdate" type="xs:unsignedShort" use="required" />
                        <xs:attribute name="ISBN" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Show:
© 2014 Microsoft