Export (0) Print
Expand All

XmlSchemaChoice Class

Allows only one of its children to appear in an instance. Represents the World Wide Web Consortium (W3C) choice (compositor) element.

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

System.Object
   System.Xml.Schema.XmlSchemaObject
      System.Xml.Schema.XmlSchemaAnnotated
         System.Xml.Schema.XmlSchemaParticle
            System.Xml.Schema.XmlSchemaGroupBase
               System.Xml.Schema.XmlSchemaChoice

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

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 a choice element.

[Visual Basic] 
Option Strict
Option Explicit

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

Class XMLSchemaExamples
    Public Shared Sub Main()
        Dim schema As New XmlSchema()
        
        ' <xs:element name="selected"/>
        Dim xeSelected As New XmlSchemaElement()
        xeSelected.Name = "selected"
        schema.Items.Add(xeSelected)
        
        ' <xs:element name="unselected"/>
        Dim xeUnselected As New XmlSchemaElement()
        xeUnselected.Name = "unselected"
        schema.Items.Add(xeUnselected)
        
        ' <xs:element name="dimpled"/>
        Dim xeDimpled As New XmlSchemaElement()
        xeDimpled.Name = "dimpled"
        schema.Items.Add(xeDimpled)
        
        ' <xs:element name="perforated"/>
        Dim xePerforated As New XmlSchemaElement()
        xePerforated.Name = "perforated"
        schema.Items.Add(xePerforated)
        
        ' <xs:complexType name="chadState">
        Dim chadState As New XmlSchemaComplexType()
        schema.Items.Add(chadState)
        chadState.Name = "chadState"
        
        ' <xs:choice minOccurs="1" maxOccurs="1">
        Dim choice As New XmlSchemaChoice()
        chadState.Particle = choice
        choice.MinOccurs = 1
        choice.MaxOccurs = 1
        
        ' <xs:element ref="selected"/>
        Dim elementSelected As New XmlSchemaElement()
        choice.Items.Add(elementSelected)
        elementSelected.RefName = New XmlQualifiedName("selected")
        
        ' <xs:element ref="unselected"/>
        Dim elementUnselected As New XmlSchemaElement()
        choice.Items.Add(elementUnselected)
        elementUnselected.RefName = New XmlQualifiedName("unselected")
        
        ' <xs:element ref="dimpled"/>
        Dim elementDimpled As New XmlSchemaElement()
        choice.Items.Add(elementDimpled)
        elementDimpled.RefName = New XmlQualifiedName("dimpled")
        
        ' <xs:element ref="perforated"/>
        Dim elementPerforated As New XmlSchemaElement()
        choice.Items.Add(elementPerforated)
        elementPerforated.RefName = New XmlQualifiedName("perforated")
        
        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 'Main
    
    
    Public Shared Sub ValidationCallbackOne(sender As Object, 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="selected"/>
        XmlSchemaElement xeSelected = new XmlSchemaElement();
        xeSelected.Name = "selected";
        schema.Items.Add(xeSelected);

        // <xs:element name="unselected"/>
        XmlSchemaElement xeUnselected = new XmlSchemaElement();
        xeUnselected.Name = "unselected";
        schema.Items.Add(xeUnselected);
        
        // <xs:element name="dimpled"/>
        XmlSchemaElement xeDimpled = new XmlSchemaElement();
        xeDimpled.Name = "dimpled";
        schema.Items.Add(xeDimpled);

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

        // <xs:complexType name="chadState">
        XmlSchemaComplexType chadState = new XmlSchemaComplexType();
        schema.Items.Add(chadState);
        chadState.Name = "chadState";
        
        // <xs:choice minOccurs="1" maxOccurs="1">
        XmlSchemaChoice choice = new XmlSchemaChoice();
        chadState.Particle = choice;
        choice.MinOccurs = 1;
        choice.MaxOccurs = 1;
        
        // <xs:element ref="selected"/>
        XmlSchemaElement elementSelected = new XmlSchemaElement();
        choice.Items.Add(elementSelected);
        elementSelected.RefName = new XmlQualifiedName("selected");

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

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

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

        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 main() {
 
        XmlSchema* schema = new XmlSchema();
    
        // <xs:element name="selected"/>
        XmlSchemaElement* xeSelected = new XmlSchemaElement();
        xeSelected->Name = S"selected";
        schema->Items->Add(xeSelected);

        // <xs:element name="unselected"/>
        XmlSchemaElement* xeUnselected = new XmlSchemaElement();
        xeUnselected->Name = S"unselected";
        schema->Items->Add(xeUnselected);
        
        // <xs:element name="dimpled"/>
        XmlSchemaElement* xeDimpled = new XmlSchemaElement();
        xeDimpled->Name = S"dimpled";
        schema->Items->Add(xeDimpled);

        // <xs:element name="perforated"/>
        XmlSchemaElement* xePerforated = new XmlSchemaElement();
        xePerforated->Name = S"perforated";
        schema->Items->Add(xePerforated);

        // <xs:complexType name="chadState">
        XmlSchemaComplexType* chadState = new XmlSchemaComplexType();
        schema->Items->Add(chadState);
        chadState->Name = S"chadState";
        
        // <xs:choice minOccurs="1" maxOccurs="1">
        XmlSchemaChoice* choice = new XmlSchemaChoice();
        chadState->Particle = choice;
        choice->MinOccurs = 1;
        choice->MaxOccurs = 1;
        
        // <xs:element ref="selected"/>
        XmlSchemaElement* elementSelected = new XmlSchemaElement();
        choice->Items->Add(elementSelected);
        elementSelected->RefName = new XmlQualifiedName(S"selected");

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

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

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

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

    static void ValidationCallbackOne(Object* /*sender*/, ValidationEventArgs* args) {

        Console::WriteLine(args->Message);
    }
};

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

[Visual Basic, C#, C++] 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="selected"/>
    <xs:element name="unselected"/>
    <xs:element name="dimpled"/>
    <xs:element name="perforated"/>
    
    <xs:complexType name="chadState">
        <xs:choice minOccurs="1" maxOccurs="1">
            <xs:element ref="selected"/>
            <xs:element ref="unselected"/>
            <xs:element ref="dimpled"/>
            <xs:element ref="perforated"/>
        </xs:choice>
    </xs:complexType>
</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

XmlSchemaChoice Members | System.Xml.Schema Namespace

Show:
© 2014 Microsoft