XmlSchema Class (System.Xml.Schema)

Switch View :
ScriptFree
.NET Framework Class Library
XmlSchema Class

An in-memory representation of an XML Schema as specified in the World Wide Web Consortium (W3C) XML Schema Part 1: Structures and XML Schema Part 2: Datatypes specifications.

Inheritance Hierarchy

System.Object
  System.Xml.Schema.XmlSchemaObject
    System.Xml.Schema.XmlSchema

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

Visual Basic
Public Class XmlSchema _
	Inherits XmlSchemaObject
C#
public class XmlSchema : XmlSchemaObject
Visual C++
public ref class XmlSchema : public XmlSchemaObject
F#
type XmlSchema =  
    class
        inherit XmlSchemaObject
    end

The XmlSchema type exposes the following members.

Constructors

  Name Description
Public method Supported by the XNA Framework Supported by Portable Class Library XmlSchema Initializes a new instance of the XmlSchema class.
Top
Properties

  Name Description
Public property Supported by the XNA Framework AttributeFormDefault Gets or sets the form for attributes declared in the target namespace of the schema.
Public property Supported by the XNA Framework AttributeGroups Gets the post-schema-compilation value of all the global attribute groups in the schema.
Public property Supported by the XNA Framework Attributes Gets the post-schema-compilation value for all the attributes in the schema.
Public property Supported by the XNA Framework BlockDefault Gets or sets the blockDefault attribute which sets the default value of the block attribute on element and complex types in the targetNamespace of the schema.
Public property Supported by the XNA Framework ElementFormDefault Gets or sets the form for elements declared in the target namespace of the schema.
Public property Supported by the XNA Framework Elements Gets the post-schema-compilation value for all the elements in the schema.
Public property Supported by the XNA Framework FinalDefault Gets or sets the finalDefault attribute which sets the default value of the final attribute on elements and complex types in the target namespace of the schema.
Public property Supported by the XNA Framework Groups Gets the post-schema-compilation value of all the groups in the schema.
Public property Supported by the XNA Framework Id Gets or sets the string ID.
Public property Supported by the XNA Framework Includes Gets the collection of included and imported schemas.
Public property Supported by the XNA Framework IsCompiled Indicates if the schema has been compiled.
Public property Supported by the XNA Framework Items Gets the collection of schema elements in the schema and is used to add new element types at the schema element level.
Public property Supported by the XNA Framework LineNumber Gets or sets the line number in the file to which the schema element refers. (Inherited from XmlSchemaObject.)
Public property Supported by the XNA Framework LinePosition Gets or sets the line position in the file to which the schema element refers. (Inherited from XmlSchemaObject.)
Public property Supported by the XNA Framework Namespaces Gets or sets the XmlSerializerNamespaces to use with this schema object. (Inherited from XmlSchemaObject.)
Public property Supported by the XNA Framework Notations Gets the post-schema-compilation value for all notations in the schema.
Public property Supported by the XNA Framework Parent Gets or sets the parent of this XmlSchemaObject. (Inherited from XmlSchemaObject.)
Public property Supported by the XNA Framework SchemaTypes Gets the post-schema-compilation value of all schema types in the schema.
Public property Supported by the XNA Framework SourceUri Gets or sets the source location for the file that loaded the schema. (Inherited from XmlSchemaObject.)
Public property Supported by the XNA Framework TargetNamespace Gets or sets the Uniform Resource Identifier (URI) of the schema target namespace.
Public property Supported by the XNA Framework UnhandledAttributes Gets and sets the qualified attributes which do not belong to the schema target namespace.
Public property Supported by the XNA Framework Version Gets or sets the version of the schema.
Top
Methods

  Name Description
Public method Compile(ValidationEventHandler) Obsolete. Compiles the XML Schema Object Model (SOM) into schema information for validation. Used to check the syntactic and semantic structure of the programmatically built SOM. Semantic validation checking is performed during compilation.
Public method Compile(ValidationEventHandler, XmlResolver) Obsolete. Compiles the XML Schema Object Model (SOM) into schema information for validation. Used to check the syntactic and semantic structure of the programmatically built SOM. Semantic validation checking is performed during compilation.
Public method Supported by the XNA Framework Supported by Portable Class Library Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Supported by the XNA Framework Supported by Portable Class Library 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 Supported by the XNA Framework Supported by Portable Class Library GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method Supported by the XNA Framework Supported by Portable Class Library MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Static member Supported by the XNA Framework Read(Stream, ValidationEventHandler) Reads an XML Schema from the supplied stream.
Public method Static member Supported by the XNA Framework Read(TextReader, ValidationEventHandler) Reads an XML Schema from the supplied TextReader.
Public method Static member Supported by the XNA Framework Read(XmlReader, ValidationEventHandler) Reads an XML Schema from the supplied XmlReader.
Public method Supported by the XNA Framework Supported by Portable Class Library ToString Returns a string that represents the current object. (Inherited from Object.)
Public method Supported by the XNA Framework Write(Stream) Writes the XML Schema to the supplied data stream.
Public method Supported by the XNA Framework Write(TextWriter) Writes the XML Schema to the supplied TextWriter.
Public method Supported by the XNA Framework Write(XmlWriter) Writes the XML Schema to the supplied XmlWriter.
Public method Supported by the XNA Framework Write(Stream, XmlNamespaceManager) Writes the XML Schema to the supplied Stream using the XmlNamespaceManager specified.
Public method Supported by the XNA Framework Write(TextWriter, XmlNamespaceManager) Writes the XML Schema to the supplied TextWriter.
Public method Supported by the XNA Framework Write(XmlWriter, XmlNamespaceManager) Writes the XML Schema to the supplied XmlWriter.
Top
Fields

  Name Description
Public field Static member Supported by the XNA Framework InstanceNamespace The XML schema instance namespace. This field is constant.
Public field Static member Supported by the XNA Framework Namespace The XML schema namespace. This field is constant.
Top
Remarks

Security note Security Note

Exceptions raised as a result of using the XmlSchema class, such as the XmlSchemaException class may contain sensitive information that should not be exposed in untrusted scenarios. For example, the SourceUri property of an XmlSchemaException returns the URI path to the schema file that caused the exception. The SourceUri property should not be exposed in untrusted scenarios. Exceptions should be properly handled so that this sensitive information is not exposed in untrusted scenarios.

Examples

The following example creates a schema definition.

Visual Basic

Option Explicit On
Option Strict On

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

Class XMLSchemaExamples
    Public Shared Sub Main()
        Dim schema As New XmlSchema()

        ' <xs:element name="cat" type="xs:string"/>
        Dim elementCat As New XmlSchemaElement()
        schema.Items.Add(elementCat)
        elementCat.Name = "cat"
        elementCat.SchemaTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")

        ' <xs:element name="dog" type="xs:string"/>
        Dim elementDog As New XmlSchemaElement()
        schema.Items.Add(elementDog)
        elementDog.Name = "dog"
        elementDog.SchemaTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")

        ' <xs:element name="redDog" substitutionGroup="dog" />
        Dim elementRedDog As New XmlSchemaElement()
        schema.Items.Add(elementRedDog)
        elementRedDog.Name = "redDog"
        elementRedDog.SubstitutionGroup = New XmlQualifiedName("dog")

        ' <xs:element name="brownDog" substitutionGroup ="dog" />
        Dim elementBrownDog As New XmlSchemaElement()
        schema.Items.Add(elementBrownDog)
        elementBrownDog.Name = "brownDog"
        elementBrownDog.SubstitutionGroup = New XmlQualifiedName("dog")

        ' <xs:element name="pets">
        Dim elementPets As New XmlSchemaElement()
        schema.Items.Add(elementPets)
        elementPets.Name = "pets"

        ' <xs:complexType>
        Dim complexType As New XmlSchemaComplexType()
        elementPets.SchemaType = complexType

        ' <xs:choice minOccurs="0" maxOccurs="unbounded">
        Dim choice As New XmlSchemaChoice()
        complexType.Particle = choice
        choice.MinOccurs = 0
        choice.MaxOccursString = "unbounded"

        ' <xs:element ref="cat"/>
        Dim catRef As New XmlSchemaElement()
        choice.Items.Add(catRef)
        catRef.RefName = New XmlQualifiedName("cat")

        ' <xs:element ref="dog"/>
        Dim dogRef As New XmlSchemaElement()
        choice.Items.Add(dogRef)
        dogRef.RefName = New XmlQualifiedName("dog")

        Dim schemaSet As New XmlSchemaSet()
        AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallbackOne

        schemaSet.Add(schema)
        schemaSet.Compile()

        Dim compiledSchema As XmlSchema = Nothing

        For Each schema1 As XmlSchema In schemaSet.Schemas()
            compiledSchema = schema1
        Next

        Dim nsmgr As New XmlNamespaceManager(New NameTable())
        nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema")
        compiledSchema.Write(Console.Out, nsmgr)


    End Sub 'Main


    Public Shared Sub ValidationCallbackOne(ByVal sender As Object, ByVal args As ValidationEventArgs)
        Console.WriteLine(args.Message)
    End Sub 'ValidationCallbackOne
End Class 'XMLSchemaExamples


C#

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

class XMLSchemaExamples
{
    public static void Main()
    {

        XmlSchema schema = new XmlSchema();

        // <xs:element name="cat" type="xs:string"/>
        XmlSchemaElement elementCat = new XmlSchemaElement();
        schema.Items.Add(elementCat);
        elementCat.Name = "cat";
        elementCat.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

        // <xs:element name="dog" type="xs:string"/>
        XmlSchemaElement elementDog = new XmlSchemaElement();
        schema.Items.Add(elementDog);
        elementDog.Name = "dog";
        elementDog.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

        // <xs:element name="redDog" substitutionGroup="dog" />
        XmlSchemaElement elementRedDog = new XmlSchemaElement();
        schema.Items.Add(elementRedDog);
        elementRedDog.Name = "redDog";
        elementRedDog.SubstitutionGroup = new XmlQualifiedName("dog");

        // <xs:element name="brownDog" substitutionGroup ="dog" />
        XmlSchemaElement elementBrownDog = new XmlSchemaElement();
        schema.Items.Add(elementBrownDog);
        elementBrownDog.Name = "brownDog";
        elementBrownDog.SubstitutionGroup = new XmlQualifiedName("dog");


        // <xs:element name="pets">
        XmlSchemaElement elementPets = new XmlSchemaElement();
        schema.Items.Add(elementPets);
        elementPets.Name = "pets";

        // <xs:complexType>
        XmlSchemaComplexType complexType = new XmlSchemaComplexType();
        elementPets.SchemaType = complexType;

        // <xs:choice minOccurs="0" maxOccurs="unbounded">
        XmlSchemaChoice choice = new XmlSchemaChoice();
        complexType.Particle = choice;
        choice.MinOccurs = 0;
        choice.MaxOccursString = "unbounded";

        // <xs:element ref="cat"/>
        XmlSchemaElement catRef = new XmlSchemaElement();
        choice.Items.Add(catRef);
        catRef.RefName = new XmlQualifiedName("cat");

        // <xs:element ref="dog"/>
        XmlSchemaElement dogRef = new XmlSchemaElement();
        choice.Items.Add(dogRef);
        dogRef.RefName = new XmlQualifiedName("dog");

        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne);
        schemaSet.Add(schema);
        schemaSet.Compile();

        XmlSchema compiledSchema = null;

        foreach (XmlSchema schema1 in schemaSet.Schemas())
        {
            compiledSchema = schema1;
        }

        XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
        nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
        compiledSchema.Write(Console.Out, nsmgr);
    }

    public static void ValidationCallbackOne(object sender, ValidationEventArgs args)
    {
        Console.WriteLine(args.Message);
    }
}


Visual C++

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

using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;

class XmlSchemaExamples
{
public:
	static void Main()
	{

		XmlSchema^ schema = gcnew XmlSchema();

		// <xs:element name="cat" type="xs:string"/>
		XmlSchemaElement^ elementCat = gcnew XmlSchemaElement();
		schema->Items->Add(elementCat);
		elementCat->Name = "cat";
		elementCat->SchemaTypeName = gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

		// <xs:element name="dog" type="xs:string"/>
		XmlSchemaElement^ elementDog = gcnew XmlSchemaElement();
		schema->Items->Add(elementDog);
		elementDog->Name = "dog";
		elementDog->SchemaTypeName = gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

		// <xs:element name="redDog" substitutionGroup="dog" />
		XmlSchemaElement^ elementRedDog = gcnew XmlSchemaElement();
		schema->Items->Add(elementRedDog);
		elementRedDog->Name = "redDog";
		elementRedDog->SubstitutionGroup = gcnew XmlQualifiedName("dog");

		// <xs:element name="brownDog" substitutionGroup ="dog" />
		XmlSchemaElement^ elementBrownDog = gcnew XmlSchemaElement();
		schema->Items->Add(elementBrownDog);
		elementBrownDog->Name = "brownDog";
		elementBrownDog->SubstitutionGroup = gcnew XmlQualifiedName("dog");


		// <xs:element name="pets">
		XmlSchemaElement^ elementPets = gcnew XmlSchemaElement();
		schema->Items->Add(elementPets);
		elementPets->Name = "pets";

		// <xs:complexType>
		XmlSchemaComplexType^ complexType = gcnew XmlSchemaComplexType();
		elementPets->SchemaType = complexType;

		// <xs:choice minOccurs="0" maxOccurs="unbounded">
		XmlSchemaChoice^ choice = gcnew XmlSchemaChoice();
		complexType->Particle = choice;
		choice->MinOccurs = 0;
		choice->MaxOccursString = "unbounded";

		// <xs:element ref="cat"/>
		XmlSchemaElement^ catRef = gcnew XmlSchemaElement();
		choice->Items->Add(catRef);
		catRef->RefName = gcnew XmlQualifiedName("cat");

		// <xs:element ref="dog"/>
		XmlSchemaElement^ dogRef = gcnew XmlSchemaElement();
		choice->Items->Add(dogRef);
		dogRef->RefName = gcnew XmlQualifiedName("dog");

		XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
		schemaSet->ValidationEventHandler += gcnew ValidationEventHandler(ValidationCallbackOne);
		schemaSet->Add(schema);
		schemaSet->Compile();

		XmlSchema^ compiledSchema;

		for each (XmlSchema^ schema1 in schemaSet->Schemas())
		{
			compiledSchema = schema1;
		}

		XmlNamespaceManager^ nsmgr = gcnew XmlNamespaceManager(gcnew NameTable());
		nsmgr->AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
		compiledSchema->Write(Console::Out, nsmgr);
	}

	static void ValidationCallbackOne(Object^ sender, ValidationEventArgs^ args)
	{
		Console::WriteLine(args->Message);
	}
};

int main()
{
	XmlSchemaExamples::Main();
	return 0;
};


The following XML file is generated for the preceding code example.



<?xml version="1.0" encoding="IBM437"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="cat" type="xs:string"/>
    <xs:element name="dog" type="xs:string"/>
    <xs:element name="redDog" type="xs:string" substitutionGroup="dog"/>
    <xs:element name="brownDog" type="xs:string" substitutionGroup ="dog" />

    <xs:element name="pets">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element ref="cat"/>
          <xs:element ref="dog"/>
        </xs:choice>
      </xs:complexType>
    </xs:element>
</xs:schema>


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library
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

Other Resources