XmlSchemaCollection Class (System.Xml.Schema)

Switch View :
ScriptFree
.NET Framework Class Library
XmlSchemaCollection Class

Note: This API is now obsolete.

Contains a cache of XML Schema definition language (XSD) and XML-Data Reduced (XDR) schemas. The XmlSchemaCollection class class is obsolete. Use XmlSchemaSet instead.

Inheritance Hierarchy

System.Object
  System.Xml.Schema.XmlSchemaCollection

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

Visual Basic
<ObsoleteAttribute("Use System.Xml.Schema.XmlSchemaSet for schema compilation and validation. http://go.microsoft.com/fwlink/?linkid=14202")> _
Public NotInheritable Class XmlSchemaCollection _
	Implements ICollection, IEnumerable
C#
[ObsoleteAttribute("Use System.Xml.Schema.XmlSchemaSet for schema compilation and validation. http://go.microsoft.com/fwlink/?linkid=14202")]
public sealed class XmlSchemaCollection : ICollection, 
	IEnumerable
Visual C++
[ObsoleteAttribute(L"Use System.Xml.Schema.XmlSchemaSet for schema compilation and validation. http://go.microsoft.com/fwlink/?linkid=14202")]
public ref class XmlSchemaCollection sealed : ICollection, 
	IEnumerable
F#
[<Sealed>]
[<ObsoleteAttribute("Use System.Xml.Schema.XmlSchemaSet for schema compilation and validation. http://go.microsoft.com/fwlink/?linkid=14202")>]
type XmlSchemaCollection =  
    class
        interface ICollection
        interface IEnumerable
    end

The XmlSchemaCollection type exposes the following members.

Constructors

  Name Description
Public method XmlSchemaCollection() Initializes a new instance of the XmlSchemaCollection class.
Public method XmlSchemaCollection(XmlNameTable) Initializes a new instance of the XmlSchemaCollection class with the specified XmlNameTable. The XmlNameTable is used when loading schemas.
Top
Properties

  Name Description
Public property Count Gets the number of namespaces defined in this collection.
Public property Item Gets the XmlSchema associated with the given namespace URI.
Public property NameTable Gets the default XmlNameTable used by the XmlSchemaCollection when loading new schemas.
Top
Methods

  Name Description
Public method Add(XmlSchema) Adds the XmlSchema to the collection.
Public method Add(XmlSchemaCollection) Adds all the namespaces defined in the given collection (including their associated schemas) to this collection.
Public method Add(String, String) Adds the schema located by the given URL into the schema collection.
Public method Add(String, XmlReader) Adds the schema contained in the XmlReader to the schema collection.
Public method Add(XmlSchema, XmlResolver) Adds the XmlSchema to the collection. The specified XmlResolver is used to resolve any external references.
Public method 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.
Public method Contains(String) Gets a value indicating whether a schema with the specified namespace is in the collection.
Public method Contains(XmlSchema) Gets a value indicating whether the targetNamespace of the specified XmlSchema is in the collection.
Public method CopyTo Copies all the XmlSchema objects from this collection into the given array starting at the given index.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetEnumerator Provides support for the "for each" style iteration over the collection of schemas.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
Events

  Name Description
Public event ValidationEventHandler Sets an event handler for receiving information about the XDR and XML schema validation errors.
Top
Extension Methods

  Name Description
Public Extension Method AsParallel Enables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension Method AsQueryable Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension Method Cast<TResult> Converts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension Method OfType<TResult> Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top
Explicit Interface Implementations

  Name Description
Explicit interface implemetation Private method ICollection.CopyTo For a description of this member, see XmlSchemaCollection.CopyTo.
Explicit interface implemetation Private property ICollection.Count For a description of this member, see XmlSchemaCollection.Count.
Explicit interface implemetation Private property ICollection.IsSynchronized For a description of this member, see ICollectionIsSynchronized().
Explicit interface implemetation Private property ICollection.SyncRoot For a description of this member, see ICollectionSyncRoot().
Explicit interface implemetation Private method IEnumerable.GetEnumerator For a description of this member, see XmlSchemaCollection.GetEnumerator.
Top
Remarks

Schemas are loaded using the Add method, at which time the schema is associated with a namespace Uniform Resource Identifier (URI). For XML Schemas, this will typically be the targetNamespace property of the schema.

Although this class stores both XML Schemas and XDR schemas, any method and property that takes or returns an XmlSchema applies to XML Schemas only.

This version of the product supports the World Wide Web Consortium (W3C) XML Schema recommendation located at http://www.w3.org/TR/xmlschema-1 and http://www.w3.org/TR/xmlschema-2. An XML Schema must reference the W3C Schema namespace http://www.w3.org/2001/XMLSchema in its schema element. See the Add method for an example.

XmlSchemaCollection can be used by XmlValidatingReader for efficient data validation.

Important note Important

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

Examples

The following example validates an XML document using the XmlSchemaCollection.

Visual Basic

Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO

public class ValidXSD 

  public shared sub Main() 
    Dim sc as XmlSchemaCollection = new XmlSchemaCollection()
    AddHandler sc.ValidationEventHandler, AddressOf ValidationCallBack
    sc.Add(nothing, "books.xsd")

    if(sc.Count > 0)
      Dim tr as XmlTextReader = new XmlTextReader("notValidXSD.xml")
      Dim rdr as XmlValidatingReader = new XmlValidatingReader(tr)

      rdr.ValidationType = ValidationType.Schema
      rdr.Schemas.Add(sc)
      AddHandler rdr.ValidationEventHandler, AddressOf ValidationCallBack
      while (rdr.Read())
      end while
    end if

  end sub

  private shared sub ValidationCallBack(sender as object, e as ValidationEventArgs) 
    Console.WriteLine("XSD Error: {0}", e.Message)
  end sub

end class


C#

using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;

public class ValidXSD {

  public static void Main() {
    XmlSchemaCollection sc = new XmlSchemaCollection();
    sc.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
    sc.Add(null, "books.xsd");

    if(sc.Count > 0)
    {
      XmlTextReader tr = new XmlTextReader("notValidXSD.xml");
      XmlValidatingReader rdr = new XmlValidatingReader(tr);

      rdr.ValidationType = ValidationType.Schema;
      rdr.Schemas.Add(sc);
      rdr.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
      while (rdr.Read());
    }

  }

  private static void ValidationCallBack(object sender, ValidationEventArgs e) {
    Console.WriteLine("Validation Error: {0}", e.Message);
  }
}


Visual C++

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;
using namespace System::IO;
public ref class ValidXSD
{
public:
   static void main()
   {
      XmlSchemaCollection^ sc = gcnew XmlSchemaCollection;
      sc->ValidationEventHandler += gcnew ValidationEventHandler( ValidationCallBack );
      sc->Add( nullptr, "books.xsd" );
      if ( sc->Count > 0 )
      {
         XmlTextReader^ tr = gcnew XmlTextReader( "notValidXSD.xml" );
         XmlValidatingReader^ rdr = gcnew XmlValidatingReader( tr );
         rdr->ValidationType = ValidationType::Schema;
         rdr->Schemas->Add( sc );
         rdr->ValidationEventHandler += gcnew ValidationEventHandler( ValidationCallBack );
         while ( rdr->Read() )
                  ;
      }
   }


private:
   static void ValidationCallBack( Object^ /*sender*/, ValidationEventArgs^ e )
   {
      Console::WriteLine( "Validation Error: {0}", e->Message );
   }

};

int main()
{
   ValidXSD::main();
}



Version Information

.NET Framework

Supported in: 1.1, 1.0
Obsolete (compiler warning) in 4
Obsolete (compiler warning) in 3.5
Obsolete (compiler warning) in 3.5 SP1
Obsolete (compiler warning) in 3.0
Obsolete (compiler warning) in 3.0 SP1
Obsolete (compiler warning) in 3.0 SP2
Obsolete (compiler warning) in 2.0
Obsolete (compiler warning) in 2.0 SP1
Obsolete (compiler warning) in 2.0 SP2

.NET Framework Client Profile

Obsolete (compiler warning) in 4
Obsolete (compiler warning) in 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also

Reference