This documentation is archived and is not being maintained.

XmlSchemaSet.Add Method (String, XmlReader)

Note: This method is new in the .NET Framework version 2.0.

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

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

public XmlSchema Add (
	string targetNamespace,
	XmlReader schemaDocument
public XmlSchema Add (
	String targetNamespace, 
	XmlReader schemaDocument
public function Add (
	targetNamespace : String, 
	schemaDocument : XmlReader
) : XmlSchema



The schema targetNamespace property, or a null reference (Nothing in Visual Basic) to use the targetNamespace specified in the schema.


The XmlReader object.

Return Value

An XmlSchema object if the schema is valid. If the schema is not valid and a ValidationEventHandler is specified, then a null reference (Nothing in Visual Basic) is returned and the appropriate validation event is raised. Otherwise, an XmlSchemaException is thrown.

Exception typeCondition


The schema is not valid.


The XmlReader object passed as a parameter is a null reference (Nothing in Visual Basic).

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.

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

  • If the XmlReader is not positioned on the root element, an XmlSchemaException 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 XmlSchemaSet; otherwise, an XmlSchemaException is thrown because the schema is not valid.

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

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

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

  • 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 a null reference (Nothing in Visual Basic) or String.Empty to the Add method instructs the XmlSchemaSet to use the target namespace defined in the schema. For an example of this behavior, see the Add method.

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

The following code example adds the books.xsd schema contained in the XmlTextReader with a target namespace of to the XmlSchemaSet.

XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add("", new XmlTextReader("books.xsd");

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0