System.Xml.Schema.XmlSchema Class

Assembly: System.Xml.dll
Namespace: System.Xml.Schema
Summary
Contains the definition of a schema. All XML Schema definition language (XSD) elements are children of the schema element. Represents the World Wide Web Consortium (W3C) schema element.
C# Syntax:
public class XmlSchema : XmlSchemaObject
Remarks
All XML Schema definition language (XSD) elements are children of the schema element.
Example
The following example creates a schema definition.
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");
        
        schema.Compile(new ValidationEventHandler(ValidationCallbackOne));
 	XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
        nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
        schema.Write(Console.Out, nsmgr);
    }

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

    

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>

    
See also:
System.Xml.Schema Namespace

System.Xml.Schema.XmlSchema Member List:

Public Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Constructs a new, empty schema.
Public Fields
InstanceNamespace Provides the instance namespace.
Namespace Provides the namespace for validation.
Public Properties
AttributeFormDefault Read-write

Gets or sets the form for attributes declared in the target namespace of the schema.
AttributeGroups Read-only

Gets the XmlSchemaObjectTable for all attribute groups in the schema, which holds the post-compilation value of the AttributeGroups property.
Attributes Read-only

Gets the XmlSchemaObjectTable, for all attributes in the schema, which holds the post-compilation value of the Attribute property.
BlockDefault Read-write

Gets or sets the BlockDefault attribute on element and complex type elements in the targetNamespace of the schema. The block attribute prevents a complex type (or element) that has the specified type of derivation from being used in place of the inherited complex type (or element). The type of derivation.
ElementFormDefault Read-write

Gets or sets the form for elements declared in the target namespace of the schema.
Elements Read-only

Gets the XmlSchemaObjectTable for all elements in the schema, which holds the post-compilation value of the Elements property.
FinalDefault Read-write

Gets or sets the default value of the final attribute on element and complex type elements in the target namespace of this schema. The final attribute prevents the specified type of derivation of an element or complex type. The type of derivation.
Groups Read-only

Gets the XmlSchemaObjectTable for all groups in the schema, which holds the post-compilation value of the Groups property.
Id Read-write

Gets or sets the string id.
Includes Read-only

Gets the collection of included and imported schemas.
IsCompiled Read-only

Gets information that indicates if the schema has been compiled.
Items Read-only

Gets the Items collection that is used to add new element types at the schema element level.
LineNumber
(inherited from System.Xml.Schema.XmlSchemaObject)
Read-write

See base class member description: System.Xml.Schema.XmlSchemaObject.LineNumber


Gets or sets the line number in the file to which the schema element refers.
LinePosition
(inherited from System.Xml.Schema.XmlSchemaObject)
Read-write

See base class member description: System.Xml.Schema.XmlSchemaObject.LinePosition


Gets or sets the line position in the file to which the schema element refers.
Namespaces
(inherited from System.Xml.Schema.XmlSchemaObject)
Read-write

See base class member description: System.Xml.Schema.XmlSchemaObject.Namespaces


Notations Read-only

Gets the XmlSchemaObjectTable for all notations in the schema.
SchemaTypes Read-only

Gets the XmlSchemaObjectTable for all schema types in the schema, which holds the post-compilation value of the SchemaTypes property.
SourceUri
(inherited from System.Xml.Schema.XmlSchemaObject)
Read-write

See base class member description: System.Xml.Schema.XmlSchemaObject.SourceUri


Gets or sets the source location for the file that loaded the schema.
TargetNamespace Read-write

Gets or sets the property for the schema target namespace. The URI reference of the namespace of this schema.
UnhandledAttributes Read-write

Gets and sets the qualified attributes which do not belong to the schema target namespace.
Version Read-write

Gets or sets the version of the schema.
Public Methods
Compile Compiles the XML Schema definition language (XSD) 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.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
Read Overloaded:
Read(Stream stream, ValidationEventHandler validationEventHandler)

Reads an XML Schema fefinition language (XSD) schema from the supplied stream.
Read Overloaded:
Read(TextReader reader, ValidationEventHandler validationEventHandler)

Reads an XSD Schema from the supplied text reader.
Read Overloaded:
Read(XmlReader reader, ValidationEventHandler validationEventHandler)

Reads an XML Schema definition language (XSD) schema from the supplied reader.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Write Overloaded:
Write(Stream stream)

Writes the XML Schema to the supplied data stream.
Write Overloaded:
Write(TextWriter writer)

Writes the XML Schema definition language (XSD) schema to the supplied TextWriter.
Write Overloaded:
Write(XmlWriter writer)

Writes the XML Schema Definition language (XSD) schema to the supplied XmlWriter.
Write Overloaded:
Write(Stream stream, XmlNamespaceManager namespaceManager)

Writes the XML Schema definition language (XSD) schema to the supplied stream.
Write Overloaded:
Write(TextWriter writer, XmlNamespaceManager namespaceManager)

Writes the XML Schema definition language (XSD) schema to the supplied TextWriter.
Write Overloaded:
Write(XmlWriter writer, XmlNamespaceManager namespaceManager)

Writes the XML Schema Definition language (XSD) schema to the supplied XmlWriter.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Xml.Schema.XmlSchema Member Details

ctor #1
Summary
Constructs a new, empty schema.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public XmlSchema();

Return to top


Field: InstanceNamespace
Summary
Provides the instance namespace.
C# Syntax:
public const string InstanceNamespace;

Return to top


Field: Namespace
Summary
Provides the namespace for validation.
C# Syntax:
public const string Namespace;

Return to top


Property: AttributeFormDefault (read-write)
Summary
Gets or sets the form for attributes declared in the target namespace of the schema.
C# Syntax:
public XmlSchemaForm AttributeFormDefault {get; set;}
Remarks
The value must be one of the XmlSchemaForm values shown in the following table.

Enum Description
Qualified Attributes from the target namespace must be qualified with the namespace prefix.
Unqualified Attributes from the target namespace are not required to be qualified with the namespace prefix.

This value is the global default for all attributes declared in the targetNamespace. Individual attributes can override this setting for the local scope using the form attribute.

Return to top


Property: AttributeGroups (read-only)
Summary
Gets the XmlSchemaObjectTable for all attribute groups in the schema, which holds the post-compilation value of the AttributeGroups property.
C# Syntax:
public XmlSchemaObjectTable AttributeGroups {get;}

Return to top


Property: Attributes (read-only)
Summary
Gets the XmlSchemaObjectTable, for all attributes in the schema, which holds the post-compilation value of the Attribute property.
C# Syntax:
public XmlSchemaObjectTable Attributes {get;}

Return to top


Property: BlockDefault (read-write)
Summary
Gets or sets the BlockDefault attribute on element and complex type elements in the targetNamespace of the schema. The block attribute prevents a complex type (or element) that has the specified type of derivation from being used in place of the inherited complex type (or element). The type of derivation.
C# Syntax:
public XmlSchemaDerivationMethod BlockDefault {get; set;}
Remarks
This value can contain #all or a list that is a subset of extension, restriction, or substitution.

Enum Description
None By default, there are no restrictions on replacements of elements in this schema.
Empty By default, there are no restrictions on replacements of elements in this schema.
Substitution By default, elements in the schema cannot be replaced by a type derived by Substitution.
Extension By default, elements in the schema cannot be replaced by a type derived by Extension.
Restriction By default, elements in the schema cannot be replaced by a type derived by Restriction.
All #all . By default, elements in the schema cannot be replaced by any type regardless of derivation method.

Return to top


Property: ElementFormDefault (read-write)
Summary
Gets or sets the form for elements declared in the target namespace of the schema.
C# Syntax:
public XmlSchemaForm ElementFormDefault {get; set;}
Remarks
The value must be one of the XmlSchemaForm values shown in the following table.

Enum Description
Qualified Elements from the target namespace must be qualified with the namespace prefix.
Unqualified Elements from the target namespace are not required to be qualified with the namespace prefix.

This value is the global default for all elements declared in the target namespace. Individual elements can override this setting for the local scope by using the form attribute.

Return to top


Property: Elements (read-only)
Summary
Gets the XmlSchemaObjectTable for all elements in the schema, which holds the post-compilation value of the Elements property.
C# Syntax:
public XmlSchemaObjectTable Elements {get;}

Return to top


Property: FinalDefault (read-write)
Summary
Gets or sets the default value of the final attribute on element and complex type elements in the target namespace of this schema. The final attribute prevents the specified type of derivation of an element or complex type. The type of derivation.
C# Syntax:
public XmlSchemaDerivationMethod FinalDefault {get; set;}
Remarks
This value can contain #all or a list that is a subset of extension or restriction.

Enum Description
None By default, there are no restrictions on type derivations in the schema.
Empty By default, there are no restrictions on type derivations in the schema.
Extension By default, elements in this schema cannot be derived by Extension. Applies only to element and complex type elements.
Restriction By default, elements in this schema cannot be derived by Restriction.
Union By default, elements in this schema cannot be derived by Union. Applies only to simple types.
List By default, elements in this schema cannot be derived by List. Applies only to simple types.
All #all . By default, elements in this schema cannot be derived by any method.

Return to top


Property: Groups (read-only)
Summary
Gets the XmlSchemaObjectTable for all groups in the schema, which holds the post-compilation value of the Groups property.
C# Syntax:
public XmlSchemaObjectTable Groups {get;}

Return to top


Property: Id (read-write)
Summary
Gets or sets the string id.
C# Syntax:
public string Id {get; set;}

Return to top


Property: Includes (read-only)
Summary
Gets the collection of included and imported schemas.
C# Syntax:
public XmlSchemaObjectCollection Includes {get;}

Return to top


Property: IsCompiled (read-only)
Summary
Gets information that indicates if the schema has been compiled.
C# Syntax:
public bool IsCompiled {get;}

Return to top


Property: Items (read-only)
Summary
Gets the Items collection that is used to add new element types at the schema element level.
C# Syntax:
public XmlSchemaObjectCollection Items {get;}
Remarks
The collection of XmlSchemaAnnotation, XmlSchemaAttribute, XmlSchemaAttributeGroup, XmlSchemaComplexType, XmlSchemaSimpleType, XmlSchemaElement, XmlSchemaGroup, or XmlSchemaNotation.

Return to top


Property: LineNumber (read-write)
Inherited
See base class member description: System.Xml.Schema.XmlSchemaObject.LineNumber

Summary
Gets or sets the line number in the file to which the schema element refers.
C# Syntax:
public int LineNumber {get; set;}
Remarks
LineNumber is used to store the line number when a schema is read from a file. This is reported through XmlSchemaException for error handling.

Return to top


Property: LinePosition (read-write)
Inherited
See base class member description: System.Xml.Schema.XmlSchemaObject.LinePosition

Summary
Gets or sets the line position in the file to which the schema element refers.
C# Syntax:
public int LinePosition {get; set;}
Remarks
This is used to store the line position when a schema is read from a file. This is reported through XmlSchemaException for error handling.

Return to top


Property: Namespaces (read-write)
Inherited
See base class member description: System.Xml.Schema.XmlSchemaObject.Namespaces

C# Syntax:
public XmlSerializerNamespaces Namespaces {get; set;}

Return to top


Property: Notations (read-only)
Summary
Gets the XmlSchemaObjectTable for all notations in the schema.
C# Syntax:
public XmlSchemaObjectTable Notations {get;}

Return to top


Property: SchemaTypes (read-only)
Summary
Gets the XmlSchemaObjectTable for all schema types in the schema, which holds the post-compilation value of the SchemaTypes property.
C# Syntax:
public XmlSchemaObjectTable SchemaTypes {get;}

Return to top


Property: SourceUri (read-write)
Inherited
See base class member description: System.Xml.Schema.XmlSchemaObject.SourceUri

Summary
Gets or sets the source location for the file that loaded the schema.
C# Syntax:
public string SourceUri {get; set;}
Remarks
Provides information for exception handling.

Return to top


Property: TargetNamespace (read-write)
Summary
Gets or sets the property for the schema target namespace. The URI reference of the namespace of this schema.
C# Syntax:
public string TargetNamespace {get; set;}
Remarks
This is the namespace of all schema components in this schema as well as any schema included using the include element. Included schemas must either have the same target namespace as the containing schema or have no target namespace specified.

Return to top


Property: UnhandledAttributes (read-write)
Summary
Gets and sets the qualified attributes which do not belong to the schema target namespace.
C# Syntax:
public XmlAttribute[] UnhandledAttributes {get; set;}

Return to top


Property: Version (read-write)
Summary
Gets or sets the version of the schema.
C# Syntax:
public string Version {get; set;}
Remarks
Optional.

Return to top


Method: Compile(
   ValidationEventHandler validationEventHandler
)
Summary
Compiles the XML Schema definition language (XSD) 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.
C# Syntax:
public void Compile(
   ValidationEventHandler validationEventHandler
);
Parameters:

validationEventHandler

The validation event handler that receives information about XML schema validation errors.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~XmlSchema();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: Read(
   Stream stream,
   ValidationEventHandler validationEventHandler
)
Summary
Reads an XML Schema fefinition language (XSD) schema from the supplied stream.
C# Syntax:
public static XmlSchema Read(
   Stream stream,
   ValidationEventHandler validationEventHandler
);
Parameters:

stream

The supplied data stream.

validationEventHandler

The validation event handler that receives information about XML Schema syntax errors.

Return to top


Overloaded Method: Read(
   TextReader reader,
   ValidationEventHandler validationEventHandler
)
Summary
Reads an XSD Schema from the supplied text reader.
C# Syntax:
public static XmlSchema Read(TextRead(
   TextReader reader,
   ValidationEventHandler validationEventHandler
);
Parameters:

reader

The supplied TextReader.

validationEventHandler

The validation event handler that receives information about the XML Schema syntax errors.

Return to top


Overloaded Method: Read(
   XmlReader reader,
   ValidationEventHandler validationEventHandler
)
Summary
Reads an XML Schema definition language (XSD) schema from the supplied reader.
C# Syntax:
public static XmlSchema Read(XmlRead(
   XmlReader reader,
   ValidationEventHandler validationEventHandler
);
Parameters:

reader

The supplied XmlReader.

validationEventHandler

The validation event handler that receives information about the XML Schema syntax errors.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: Write(
   Stream stream
)
Summary
Writes the XML Schema to the supplied data stream.
C# Syntax:
public void Write(
   Stream stream
);
Parameters:

stream

The supplied datat stream.

Return to top


Overloaded Method: Write(
   TextWriter writer
)
Summary
Writes the XML Schema definition language (XSD) schema to the supplied TextWriter.
C# Syntax:
public void Write(TextWrite(
   TextWriter writer
);
Parameters:

writer

The supplied TextWriter.

Return to top


Overloaded Method: Write(
   XmlWriter writer
)
Summary
Writes the XML Schema Definition language (XSD) schema to the supplied XmlWriter.
C# Syntax:
public void Write(XmlWrite(
   XmlWriter writer
);
Parameters:

writer

The supplied XmlWriter.

Return to top


Overloaded Method: Write(
   Stream stream,
   XmlNamespaceManager namespaceManager
)
Summary
Writes the XML Schema definition language (XSD) schema to the supplied stream.
C# Syntax:
public void Write(
   Stream stream,
   XmlNamespaceManager namespaceManager
);
Parameters:

stream

The supplied data stream.

namespaceManager

The XmlNamespaceManager.

Return to top


Overloaded Method: Write(
   TextWriter writer,
   XmlNamespaceManager namespaceManager
)
Summary
Writes the XML Schema definition language (XSD) schema to the supplied TextWriter.
C# Syntax:
public void Write(TextWrite(
   TextWriter writer,
   XmlNamespaceManager namespaceManager
);
Parameters:

writer

The supplied TextWriter.

namespaceManager

The XmlNamespaceManager.

Return to top


Overloaded Method: Write(
   XmlWriter writer,
   XmlNamespaceManager namespaceManager
)
Summary
Writes the XML Schema Definition language (XSD) schema to the supplied XmlWriter.
C# Syntax:
public void Write(XmlWrite(
   XmlWriter writer,
   XmlNamespaceManager namespaceManager
);
Parameters:

writer

The supplied XmlWriter.

namespaceManager

The XmlNamespaceManager.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.