XmlSchemaCollection.Add Method

Definition

Adds the given schema into the schema collection.

Overloads

Add(XmlSchema)

Adds the XmlSchema to the collection.

Add(XmlSchemaCollection)

Adds all the namespaces defined in the given collection (including their associated schemas) to this collection.

Add(String, String)

Adds the schema located by the given URL into the schema collection.

Add(String, XmlReader)

Adds the schema contained in the XmlReader to the schema collection.

Add(XmlSchema, XmlResolver)

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

Add(String, XmlReader, XmlResolver)

Adds the schema contained in the XmlReader to the schema collection. The specified XmlResolver is used to resolve any external resources.

Remarks

Important

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

Add(XmlSchema)

Source:
XmlSchemaCollection.cs
Source:
XmlSchemaCollection.cs
Source:
XmlSchemaCollection.cs

Adds the XmlSchema to the collection.

C#
public System.Xml.Schema.XmlSchema? Add(System.Xml.Schema.XmlSchema schema);
C#
public System.Xml.Schema.XmlSchema Add(System.Xml.Schema.XmlSchema schema);

Parameters

schema
XmlSchema

The XmlSchema to add to the collection.

Returns

The XmlSchema object.

Remarks

The targetNamespace attribute is used to identify this schema.

If the schema being added contains references to other namespaces (through include and import elements), the trust level of the application determines how these other namespaces are resolved. (In the .NET Framework version 1.0, a default XmlUrlResolver was always used).

Fully trusted code: A default XmlUrlResolver with no user credentials is used to resolve any external resources. 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. If these external resources are located on a network resource that requires authentication, use an overload that takes an XmlResolver as one of its arguments and specify an XmlResolver with the necessary credentials.

Semi-trusted code: External references are not resolved.

Note

If the XmlSchemaCollection is being accessed using the XmlValidatingReader.Schemas property, the Add method uses the XmlResolver specified by the XmlValidatingReader.XmlResolver property.

Important

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

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(XmlSchemaCollection)

Source:
XmlSchemaCollection.cs
Source:
XmlSchemaCollection.cs
Source:
XmlSchemaCollection.cs

Adds all the namespaces defined in the given collection (including their associated schemas) to this collection.

C#
public void Add(System.Xml.Schema.XmlSchemaCollection schema);

Parameters

schema
XmlSchemaCollection

The XmlSchemaCollection you want to add to this collection.

Remarks

If the schema being added contains references to other namespaces (through include and import elements or the x-schema attribute), the trust level of the application determines how these other namespaces are resolved. (In the .NET Framework version 1.0, a default XmlUrlResolver was always used).

Fully trusted code: A default XmlUrlResolver with no user credentials is used to resolve any external resources. 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. If these external resources are located on a network resource that requires authentication, use an overload that takes an XmlResolver as one of its arguments and specify an XmlResolver with the necessary credentials.

Semi-trusted code: External references are not resolved.

Note

If the XmlSchemaCollection is being accessed using the XmlValidatingReader.Schemas property, the Add method uses the XmlResolver specified by the XmlValidatingReader.XmlResolver property.

Important

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(String, String)

Source:
XmlSchemaCollection.cs
Source:
XmlSchemaCollection.cs
Source:
XmlSchemaCollection.cs

Adds the schema located by the given URL into the schema collection.

C#
public System.Xml.Schema.XmlSchema? Add(string? ns, string uri);
C#
public System.Xml.Schema.XmlSchema Add(string ns, string uri);

Parameters

ns
String

The namespace URI associated with the schema. For XML Schemas, this will typically be the targetNamespace.

uri
String

The URL that specifies the schema to load.

Returns

The XmlSchema added to the schema collection; null if the schema being added is an XDR schema or if there are compilation errors in the schema.

Exceptions

The schema is not a valid schema.

Examples

The following example validates three XML files using schemas stored in the XmlSchemaCollection.

C#
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

public class SchemaCollectionSample
{
  private const String doc1 = "booksSchema.xml";
  private const String doc2 = "booksSchemaFail.xml";
  private const String doc3 = "newbooks.xml";
  private const String schema = "books.xsd";
  private const String schema1 = "schema1.xdr";

  private XmlTextReader reader=null;
  private XmlValidatingReader vreader = null;
  private Boolean m_success = true;

  public SchemaCollectionSample ()
  {
    //Load the schema collection.
    XmlSchemaCollection xsc = new XmlSchemaCollection();
    xsc.Add("urn:bookstore-schema", schema);  //XSD schema
    xsc.Add("urn:newbooks-schema", schema1);  //XDR schema

    //Validate the files using schemas stored in the collection.
    Validate(doc1, xsc); //Should pass.
    Validate(doc2, xsc); //Should fail.
    Validate(doc3, xsc); //Should fail.
  }

  public static void Main ()
  {
      SchemaCollectionSample validation = new SchemaCollectionSample();
  }

  private void Validate(String filename, XmlSchemaCollection xsc)
  {

     m_success = true;
     Console.WriteLine();
     Console.WriteLine("Validating XML file {0}...", filename.ToString());
     reader = new XmlTextReader (filename);

     //Create a validating reader.
    vreader = new XmlValidatingReader (reader);

     //Validate using the schemas stored in the schema collection.
     vreader.Schemas.Add(xsc);

     //Set the validation event handler
     vreader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
     //Read and validate the XML data.
     while (vreader.Read()){}
     Console.WriteLine ("Validation finished. Validation {0}", (m_success==true ? "successful" : "failed"));
     Console.WriteLine();

     //Close the reader.
     vreader.Close();
  }

  private void ValidationCallBack (object sender, ValidationEventArgs args)
  {
     m_success = false;

     Console.Write("\r\n\tValidation error: " + args.Message);
  }
}

The sample uses the following five input files:

booksSchema.xml

XML

<?xml version='1.0'?>
 <bookstore xmlns="urn:bookstore-schema">
   <book genre="autobiography">
     <title>The Autobiography of Benjamin Franklin</title>
     <author>
       <first-name>Benjamin</first-name>
       <last-name>Franklin</last-name>
     </author>
     <price>8.99</price>
   </book>
   <book genre="novel">
     <title>The Confidence Man</title>
     <author>
       <first-name>Herman</first-name>
       <last-name>Melville</last-name>
     </author>
     <price>11.99</price>
   </book>
 </bookstore>

booksSchemaFail.xml

XML

<?xml version='1.0'?>
 <bookstore xmlns="urn:bookstore-schema">
   <book>
     <author>
       <first-name>Benjamin</first-name>
       <last-name>Franklin</last-name>
     </author>
   </book>
   <book genre="novel">
     <title>The Confidence Man</title>
     <author>
       <first-name>Herman</first-name>
       <last-name>Melville</last-name>
     </author>
     <price>11.99</price>
   </book>
   <book genre="philosophy">
     <title>The Gorgias</title>
     <author>
       <name>Plato</name>
     </author>
     <price>9.99</price>
   </book>
 </bookstore>

newbooks.xml

XML

<?xml version='1.0'?>
<bookstore xmlns="urn:newbooks-schema">
  <book genre="novel" style="hardcover">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" style="other">
    <title>The Poisonwood Bible</title>
    <author>
      <first-name>Barbara</first-name>
      <last-name>Kingsolver</last-name>
    </author>
    <price>11.99</price>
  </book>
</bookstore>

books.xsd

XML

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
     xmlns="urn:bookstore-schema"
     elementFormDefault="qualified"
     targetNamespace="urn:bookstore-schema">
 
  <xsd:element name="bookstore" type="bookstoreType"/>
 
  <xsd:complexType name="bookstoreType">
   <xsd:sequence maxOccurs="unbounded">
    <xsd:element name="book"  type="bookType"/>
   </xsd:sequence>
  </xsd:complexType>
 
  <xsd:complexType name="bookType">
   <xsd:sequence>
    <xsd:element name="title" type="xsd:string"/>
    <xsd:element name="author" type="authorName"/>
    <xsd:element name="price"  type="xsd:decimal"/>
   </xsd:sequence>
   <xsd:attribute name="genre" type="xsd:string"/>
  </xsd:complexType>
 
  <xsd:complexType name="authorName">
   <xsd:sequence>
    <xsd:element name="first-name"  type="xsd:string"/>
    <xsd:element name="last-name" type="xsd:string"/>
   </xsd:sequence>
  </xsd:complexType>
 
 </xsd:schema>

schema1.xdr

XML

<?xml version="1.0"?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data"
        xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <ElementType name="first-name" content="textOnly"/>
  <ElementType name="last-name" content="textOnly"/>
  <ElementType name="name" content="textOnly"/>
  <ElementType name="price" content="textOnly" dt:type="fixed.14.4"/>
  <ElementType name="author" content="eltOnly" order="one">
    <group order="seq">
      <element type="name"/>
    </group>
    <group order="seq">
      <element type="first-name"/>
      <element type="last-name"/>
    </group>
  </ElementType>
  <ElementType name="title" content="textOnly"/>
  <AttributeType name="genre" dt:type="string"/>
  <AttributeType name="style" dt:type="enumeration"
        dt:values="paperback hardcover"/>
  <ElementType name="book" content="eltOnly">
    <attribute type="genre" required="yes"/>
    <attribute type="style" required="yes"/>
    <element type="title"/>
    <element type="author"/>
    <element type="price"/>
  </ElementType>
  <ElementType name="bookstore" content="eltOnly">
    <element type="book"/>
  </ElementType>
</Schema>

Remarks

If ns has already been associated with another schema in the collection, the schema being added replaces the original schema in the collection. For example, in the following C# code, authors.xsd is removed from the collection and names.xsd is added.

schemaColl.Add("urn:author", "authors.xsd");  
schemaColl.Add("urn:author", "names.xsd");  

If ns is null and the schema being added is an XML Schema, the Add method uses the targetNamespace defined in the XML Schema to identify the schema in the collection. If the schema being added contains references to other namespaces (through include and import elements or the x-schema attribute), the trust level of the application determines how these other namespaces are resolved. (In the .NET Framework version 1.0, a default XmlUrlResolver was always used).

Fully trusted code: A default XmlUrlResolver with no user credentials is used to resolve any external resources. 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. If these external resources are located on a network resource that requires authentication, use an overload that takes an XmlResolver as one of its arguments and specify an XmlResolver with the necessary credentials.

Semi-trusted code: External references are not resolved.

Note

If the XmlSchemaCollection is being accessed using the XmlValidatingReader.Schemas property, the Add method uses the XmlResolver specified by the XmlValidatingReader.XmlResolver property.

Important

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(String, XmlReader)

Source:
XmlSchemaCollection.cs
Source:
XmlSchemaCollection.cs
Source:
XmlSchemaCollection.cs

Adds the schema contained in the XmlReader to the schema collection.

C#
public System.Xml.Schema.XmlSchema? Add(string? ns, System.Xml.XmlReader reader);
C#
public System.Xml.Schema.XmlSchema Add(string ns, System.Xml.XmlReader reader);

Parameters

ns
String

The namespace URI associated with the schema. For XML Schemas, this will typically be the targetNamespace.

reader
XmlReader

XmlReader containing the schema to add.

Returns

The XmlSchema added to the schema collection; null if the schema being added is an XDR schema or if there are compilation errors in the schema.

Exceptions

The schema is not a valid schema.

Remarks

If ns has already been associated with another schema in the collection, the schema being added replaces the original schema in the collection.

If ns is null and the schema being added is an XML Schema, the Add method uses the targetNamespace defined in the XML Schema to identify the schema in the collection.

If the schema being added contains references to other namespaces (through include and import elements or the x-schema attribute), the trust level of the application determines how these other namespaces are resolved. (In the .NET Framework version 1.0, a default XmlUrlResolver was always used).

Fully trusted code: A default XmlUrlResolver with no user credentials is used to resolve any external resources. 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. If these external resources are located on a network resource that requires authentication, use an overload that takes an XmlResolver as one of its arguments and specify an XmlResolver with the necessary credentials.

Semi-trusted code: External references are not resolved.

Note

If the XmlSchemaCollection is being accessed using the XmlValidatingReader.Schemas property, the Add method uses the XmlResolver specified by the XmlValidatingReader.XmlResolver property.

Important

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(XmlSchema, XmlResolver)

Source:
XmlSchemaCollection.cs
Source:
XmlSchemaCollection.cs
Source:
XmlSchemaCollection.cs

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

C#
public System.Xml.Schema.XmlSchema? Add(System.Xml.Schema.XmlSchema schema, System.Xml.XmlResolver? resolver);
C#
public System.Xml.Schema.XmlSchema Add(System.Xml.Schema.XmlSchema schema, System.Xml.XmlResolver resolver);

Parameters

schema
XmlSchema

The XmlSchema to add to the collection.

resolver
XmlResolver

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

Returns

The XmlSchema added to the schema collection.

Exceptions

The schema is not a valid schema.

Examples

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.

C#
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);

Remarks

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.

Important

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

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(String, XmlReader, XmlResolver)

Source:
XmlSchemaCollection.cs
Source:
XmlSchemaCollection.cs
Source:
XmlSchemaCollection.cs

Adds the schema contained in the XmlReader to the schema collection. The specified XmlResolver is used to resolve any external resources.

C#
public System.Xml.Schema.XmlSchema? Add(string? ns, System.Xml.XmlReader reader, System.Xml.XmlResolver? resolver);
C#
public System.Xml.Schema.XmlSchema Add(string ns, System.Xml.XmlReader reader, System.Xml.XmlResolver resolver);

Parameters

ns
String

The namespace URI associated with the schema. For XML Schemas, this will typically be the targetNamespace.

reader
XmlReader

XmlReader containing the schema to add.

resolver
XmlResolver

The XmlResolver used to resolve namespaces referenced in include and import elements or x-schema attribute (XDR schemas). If this is null, external references are not resolved.

Returns

The XmlSchema added to the schema collection; null if the schema being added is an XDR schema or if there are compilation errors in the schema.

Exceptions

The schema is not a valid schema.

Remarks

If ns has already been associated with another schema in the collection, the schema being added replaces the original schema in the collection.

If ns is null and the schema being added is an XML Schema, the Add method uses the targetNamespace attribute defined in the XML Schema to identify the schema in the collection.

If the schema being added contains references to other namespaces (through include and import elements or the x-schema attribute), 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.

Important

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

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1