XmlSchemaElement XmlSchemaElement XmlSchemaElement XmlSchemaElement Class


Represents the element element from XML Schema as specified by the World Wide Web Consortium (W3C). This class is the base class for all particle types and is used to describe an element in an XML document.

public ref class XmlSchemaElement : System::Xml::Schema::XmlSchemaParticle
public class XmlSchemaElement : System.Xml.Schema.XmlSchemaParticle
type XmlSchemaElement = class
    inherit XmlSchemaParticle
Public Class XmlSchemaElement
Inherits XmlSchemaParticle


The following example creates the element element.

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

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

class XmlSchemaExamples

    static void Main()
        XmlSchema^ schema = gcnew XmlSchema();

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

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

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

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

        // <xs:element name="pets">
        XmlSchemaElement^ elementPets = gcnew XmlSchemaElement();
        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();
        catRef->RefName = gcnew XmlQualifiedName("cat");

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

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

        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)

int main()
    return 0;
using System;
using System.Xml;
using System.Xml.Schema;

class XMLSchemaExamples
    public static void Main()

        XmlSchema schema = new XmlSchema();

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

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

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

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

        // <xs:element name="pets">
        XmlSchemaElement elementPets = new XmlSchemaElement();
        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();
        catRef.RefName = new XmlQualifiedName("cat");

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

        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne);

        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)
Imports System.Xml
Imports System.Xml.Schema

Class XMLSchemaExamples
    Public Shared Sub Main()

        Dim schema As New XmlSchema()

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

        ' <xs:element name="dog" type="string"/>
        Dim elementDog As New XmlSchemaElement()
        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()
        elementRedDog.Name = "redDog"
        elementRedDog.SubstitutionGroup = New XmlQualifiedName("dog")

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

        ' <xs:element name="pets">
        Dim elementPets As New XmlSchemaElement()
        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()
        catRef.RefName = New XmlQualifiedName("cat")

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

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


        Dim compiledSchema As XmlSchema = Nothing

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

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

    End Sub

    Public Shared Sub ValidationCallbackOne(ByVal sender As Object, ByVal args As ValidationEventArgs)
    End Sub

End Class

The following XML file is used 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" substitutionGroup="dog" />
	<xs:element name="brownDog" substitutionGroup ="dog" />

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



  • Do not use schemas from unknown or untrusted sources or locations. Doing so will compromise the security of your code.
  • XML schemas (including inline schemas) are inherently vulnerable to denial of service attacks; do not accept them in untrusted scenarios.
  • Schema validation error messages and exceptions may expose sensitive information about the content model or URI paths to the schema file. Be careful not to expose this information to untrusted callers.


XmlSchemaElement() XmlSchemaElement() XmlSchemaElement() XmlSchemaElement()

Initializes a new instance of the XmlSchemaElement class.


Annotation Annotation Annotation Annotation

Gets or sets the annotation property.

(Inherited from XmlSchemaAnnotated)
Block Block Block Block

Gets or sets a Block derivation.

BlockResolved BlockResolved BlockResolved BlockResolved

Gets the post-compilation value of the Block property.

Constraints Constraints Constraints Constraints

Gets the collection of constraints on the element.

DefaultValue DefaultValue DefaultValue DefaultValue

Gets or sets the default value of the element if its content is a simple type or content of the element is textOnly.

ElementSchemaType ElementSchemaType ElementSchemaType ElementSchemaType

Gets an XmlSchemaType object representing the type of the element based on the SchemaType or SchemaTypeName values of the element.

ElementType ElementType ElementType ElementType

Gets a common language runtime (CLR) object based on the XmlSchemaElement or XmlSchemaElement of the element, which holds the post-compilation value of the ElementType property.

Final Final Final Final

Gets or sets the Final property to indicate that no further derivations are allowed.

FinalResolved FinalResolved FinalResolved FinalResolved

Gets the post-compilation value of the Final property.

FixedValue FixedValue FixedValue FixedValue

Gets or sets the fixed value.

Form Form Form Form

Gets or sets the form for the element.

Id Id Id Id

Gets or sets the string id.

(Inherited from XmlSchemaAnnotated)
IsAbstract IsAbstract IsAbstract IsAbstract

Gets or sets information to indicate if the element can be used in an instance document.

IsNillable IsNillable IsNillable IsNillable

Gets or sets information that indicates if xsi:nil can occur in the instance data. Indicates if an explicit nil value can be assigned to the element.

LineNumber LineNumber LineNumber LineNumber

Gets or sets the line number in the file to which the schema element refers.

(Inherited from XmlSchemaObject)
LinePosition LinePosition LinePosition LinePosition

Gets or sets the line position in the file to which the schema element refers.

(Inherited from XmlSchemaObject)
MaxOccurs MaxOccurs MaxOccurs MaxOccurs

Gets or sets the maximum number of times the particle can occur.

(Inherited from XmlSchemaParticle)
MaxOccursString MaxOccursString MaxOccursString MaxOccursString

Gets or sets the number as a string value. Maximum number of times the particle can occur.

(Inherited from XmlSchemaParticle)
MinOccurs MinOccurs MinOccurs MinOccurs

Gets or sets the minimum number of times the particle can occur.

(Inherited from XmlSchemaParticle)
MinOccursString MinOccursString MinOccursString MinOccursString

Gets or sets the number as a string value. The minimum number of times the particle can occur.

(Inherited from XmlSchemaParticle)
Name Name Name Name

Gets or sets the name of the element.

Namespaces Namespaces Namespaces Namespaces

Gets or sets the XmlSerializerNamespaces to use with this schema object.

(Inherited from XmlSchemaObject)
Parent Parent Parent Parent

Gets or sets the parent of this XmlSchemaObject.

(Inherited from XmlSchemaObject)
QualifiedName QualifiedName QualifiedName QualifiedName

Gets the actual qualified name for the given element.

RefName RefName RefName RefName

Gets or sets the reference name of an element declared in this schema (or another schema indicated by the specified namespace).

SchemaType SchemaType SchemaType SchemaType

Gets or sets the type of the element. This can either be a complex type or a simple type.

SchemaTypeName SchemaTypeName SchemaTypeName SchemaTypeName

Gets or sets the name of a built-in data type defined in this schema or another schema indicated by the specified namespace.

SourceUri SourceUri SourceUri SourceUri

Gets or sets the source location for the file that loaded the schema.

(Inherited from XmlSchemaObject)
SubstitutionGroup SubstitutionGroup SubstitutionGroup SubstitutionGroup

Gets or sets the name of an element that is being substituted by this element.

UnhandledAttributes UnhandledAttributes UnhandledAttributes UnhandledAttributes

Gets or sets the qualified attributes that do not belong to the current schema's target namespace.

(Inherited from XmlSchemaAnnotated)


Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to