XmlSchemaCollection.Add Method (XmlSchema, XmlResolver)


Adds the XmlSchema to the collection. The specified XmlResolver is used to resolve any external references.

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

public XmlSchema Add(
	XmlSchema schema,
	XmlResolver resolver


Type: System.Xml.Schema.XmlSchema

The XmlSchema to add to the collection.

Type: System.Xml.XmlResolver

The XmlResolver used to resolve namespaces referenced in include and import elements. If this is null, external references are not resolved.

Return Value

Type: System.Xml.Schema.XmlSchema

The XmlSchema added to the schema collection.

Exception Condition

The schema is not a valid schema.

The targetNamespace attribute is used to identify this schema.

If the schema contains include and import elements that reference other namespaces, the schemas for these other namespaces are loaded for validation purposes only. Unlike the original schema, these other schemas are not explicitly added to the schema collection. As a result, they are not accessible using any of the collection methods or properties.


The XmlSchemaCollection class is obsolete in the .NET Framework version 2.0 and has been replaced by the XmlSchemaSet class.

The following example adds a schema to the collection. An XmlUrlResolver is passed to the Add method which sets the necessary credentials required to access any external resources referenced in the schema.

XmlSchemaCollection sc = new XmlSchemaCollection();
sc.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

// Create a resolver with the necessary credentials.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Add the new schema to the collection.
sc.Add("", new XmlTextReader("sample.xsd"), resolver);

.NET Framework
Available since 1.1
Return to top