Export (0) Print
Expand All

XmlSchemaElement Class

Base class for all particle types. Used to describe an element in a XML document. Represents the World Wide Web Consortium (W3C) element element.

For a list of all members of this type, see XmlSchemaElement Members.

System.Object
   System.Xml.Schema.XmlSchemaObject
      System.Xml.Schema.XmlSchemaAnnotated
         System.Xml.Schema.XmlSchemaParticle
            System.Xml.Schema.XmlSchemaElement

[Visual Basic]
Public Class XmlSchemaElement
   Inherits XmlSchemaParticle
[C#]
public class XmlSchemaElement : XmlSchemaParticle
[C++]
public __gc class XmlSchemaElement : public XmlSchemaParticle
[JScript]
public class XmlSchemaElement extends XmlSchemaParticle

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.

Example

[Visual Basic, C#, C++] The following example creates the element element.

[Visual Basic] 
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="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="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")

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

    End Sub

  Public Shared Sub ValidationCallbackOne(sender as object, args as ValidationEventArgs) 
    Console.WriteLine(args.Message)
  End Sub

End Class

[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="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="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);
    }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>

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

__gc class XMLSchemaExamples {
public:
    static void ValidationCallbackOne(Object* /*sender*/, ValidationEventArgs* args) {
        Console::WriteLine(args->Message);
    }
};

int main() {

    XmlSchema* schema = new XmlSchema();

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

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

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


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


    // <xs:element name="pets">
    XmlSchemaElement* elementPets = new XmlSchemaElement();
    schema->Items->Add(elementPets);
    elementPets->Name = S"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 = S"unbounded";

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

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

    schema->Compile(new ValidationEventHandler(0, XMLSchemaExamples::ValidationCallbackOne));
    XmlNamespaceManager* nsmgr = new XmlNamespaceManager(new NameTable());
    nsmgr->AddNamespace(S"xs", S"http://www.w3.org/2001/XMLSchema");
    schema->Write(Console::Out, nsmgr);
}

[Visual Basic, C#, C++] 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:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element ref="cat"/>
          <xs:element ref="dog"/>
        </xs:choice>
      </xs:complexType>
    </xs:element>
</xs:schema>

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Xml.Schema

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Xml (in System.Xml.dll)

See Also

XmlSchemaElement Members | System.Xml.Schema Namespace

Show:
© 2014 Microsoft