XmlSchemaSet::Add Method (String^, String^)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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)

XmlSchema^ Add(
	String^ targetNamespace,
	String^ schemaUri


Type: System::String^

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

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.

Exception Condition

The schema is not valid.


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.


    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.

No code example is currently available or this language may not be supported.

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" />

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.

No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

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:element maxOccurs="unbounded" name="book">
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="author">
                                        <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:element name="price" type="xs:decimal" />
                        <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" />

.NET Framework
Available since 2.0
Return to top