Lire en anglais

Partager via


XmlSchemaChoice Classe

Définition

Représente l’élément choice (compositeur) issu d’un schéma XML comme spécifié par le W3C (World Wide Web Consortium). choice ne permet l'apparition que d'un seul de ses enfants par instance.

C#
public class XmlSchemaChoice : System.Xml.Schema.XmlSchemaGroupBase
Héritage

Exemples

L’exemple suivant crée un choice élément .

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");

        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);
    }
}

Le fichier XML suivant est généré pour l’exemple de code précédent.

XML

<?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>

Constructeurs

XmlSchemaChoice()

Initialise une nouvelle instance de la classe XmlSchemaChoice.

Propriétés

Annotation

Obtient ou définit la propriété annotation.

(Hérité de XmlSchemaAnnotated)
Id

Obtient ou définit l'ID de chaîne.

(Hérité de XmlSchemaAnnotated)
Items

Obtient la collection des éléments contenus avec le compositeur (choice) : XmlSchemaElement, XmlSchemaGroupRef, XmlSchemaChoice, XmlSchemaSequence ou XmlSchemaAny.

LineNumber

Obtient ou définit le numéro de la ligne du fichier à laquelle l'élément schema fait référence.

(Hérité de XmlSchemaObject)
LinePosition

Obtient ou définit la position de la ligne du fichier à laquelle l'élément schema fait référence.

(Hérité de XmlSchemaObject)
MaxOccurs

Obtient ou définit le nombre maximal de fois que la particule peut se présenter.

(Hérité de XmlSchemaParticle)
MaxOccursString

Obtient ou définit le nombre sous forme d'une valeur de chaîne. Nombre maximal d'occurrences de la particule.

(Hérité de XmlSchemaParticle)
MinOccurs

Obtient ou définit le nombre minimal de fois que la particule peut se présenter.

(Hérité de XmlSchemaParticle)
MinOccursString

Obtient ou définit le nombre sous forme d'une valeur de chaîne. Nombre minimal de fois que la particule peut se présenter.

(Hérité de XmlSchemaParticle)
Namespaces

Obtient ou définit le XmlSerializerNamespaces à utiliser avec cet objet de schéma.

(Hérité de XmlSchemaObject)
Parent

Obtient ou définit le parent de ce XmlSchemaObject.

(Hérité de XmlSchemaObject)
SourceUri

Obtient ou définit l'emplacement de la source pour le fichier qui a chargé le schéma.

(Hérité de XmlSchemaObject)
UnhandledAttributes

Obtient ou définit les attributs qualifiés qui n'appartiennent pas à l'espace de noms cible du schéma en cours.

(Hérité de XmlSchemaAnnotated)

Méthodes

Equals(Object)

Détermine si l'objet spécifié est égal à l'objet actuel.

(Hérité de Object)
GetHashCode()

Fait office de fonction de hachage par défaut.

(Hérité de Object)
GetType()

Obtient le Type de l'instance actuelle.

(Hérité de Object)
MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
ToString()

Retourne une chaîne qui représente l'objet actuel.

(Hérité de Object)

S’applique à

Produit Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1