XmlSchemaElement Classe

Definizione

Rappresenta l'elemento element da XML Schema come specificato dal World Wide Web Consortium (W3C). Questa classe è la classe base per tutti i tipi di particelle e viene usata per descrivere un elemento in un documento XML.

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
Ereditarietà

Esempio

Nell'esempio seguente viene creato l'elemento element .

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

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

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

        schemaSet.Add(schema)
        schemaSet.Compile()

        Dim compiledSchema As XmlSchema = Nothing

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

        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)
        Console.WriteLine(args.Message)
    End Sub

End Class

Il file XML seguente viene usato per l'esempio di codice precedente.

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

Commenti

Importante

  • Non usare schemi da origini o percorsi sconosciuti o non attendibili. In questo modo verrà compromessa la sicurezza del codice.
  • Gli XML Schema (inclusi gli schemi inline) sono intrinsecamente vulnerabili agli attacchi Denial of Service; non accettarli in scenari non attendibili.
  • I messaggi di errore di convalida dello schema e le eccezioni possono esporre informazioni riservate sul modello di contenuto o i percorsi URI al file di schema. Prestare attenzione a non esporre queste informazioni ai chiamanti non attendibili.

Costruttori

Nome Descrizione
XmlSchemaElement()

Inizializza una nuova istanza della classe XmlSchemaElement.

Proprietà

Nome Descrizione
Annotation

Ottiene o imposta la annotation proprietà .

(Ereditato da XmlSchemaAnnotated)
Block

Ottiene o imposta una Block derivazione.

BlockResolved

Ottiene il valore di post-compilazione della Block proprietà .

Constraints

Ottiene la raccolta di vincoli sull'elemento .

DefaultValue

Ottiene o imposta il valore predefinito dell'elemento se il contenuto è un tipo o contenuto semplice dell'elemento è textOnly.

ElementSchemaType

Ottiene un XmlSchemaType oggetto che rappresenta il tipo dell'elemento in base ai SchemaType valori o SchemaTypeName dell'elemento.

ElementType
Obsoleti.
Obsoleti.
Obsoleti.

Ottiene un oggetto CLR (Common Language Runtime) basato su XmlSchemaElement o XmlSchemaElement sull'elemento , che contiene il valore di post-compilazione della ElementType proprietà.

Final

Ottiene o imposta la Final proprietà per indicare che non sono consentite altre derivazioni.

FinalResolved

Ottiene il valore di post-compilazione della Final proprietà .

FixedValue

Ottiene o imposta il valore fisso.

Form

Ottiene o imposta il form per l'elemento .

Id

Ottiene o imposta l'ID stringa.

(Ereditato da XmlSchemaAnnotated)
IsAbstract

Ottiene o imposta informazioni per indicare se l'elemento può essere utilizzato in un documento di istanza.

IsNillable

Ottiene o imposta informazioni che indicano se xsi:nil possono verificarsi nei dati dell'istanza. Indica se è possibile assegnare un valore nil esplicito all'elemento .

LineNumber

Ottiene o imposta il numero di riga nel file a cui fa riferimento l'elemento schema .

(Ereditato da XmlSchemaObject)
LinePosition

Ottiene o imposta la posizione della riga nel file a cui fa riferimento l'elemento schema .

(Ereditato da XmlSchemaObject)
MaxOccurs

Ottiene o imposta il numero massimo di volte in cui può verificarsi la particella.

(Ereditato da XmlSchemaParticle)
MaxOccursString

Ottiene o imposta il numero come valore stringa. Numero massimo di volte in cui può verificarsi la particella.

(Ereditato da XmlSchemaParticle)
MinOccurs

Ottiene o imposta il numero minimo di volte in cui può verificarsi la particella.

(Ereditato da XmlSchemaParticle)
MinOccursString

Ottiene o imposta il numero come valore stringa. Numero minimo di volte in cui può verificarsi la particella.

(Ereditato da XmlSchemaParticle)
Name

Ottiene o imposta il nome dell'elemento.

Namespaces

Ottiene o imposta l'oggetto XmlSerializerNamespaces da utilizzare con questo oggetto schema.

(Ereditato da XmlSchemaObject)
Parent

Ottiene o imposta l'elemento padre dell'oggetto XmlSchemaObject.

(Ereditato da XmlSchemaObject)
QualifiedName

Ottiene il nome completo effettivo per l'elemento specificato.

RefName

Ottiene o imposta il nome di riferimento di un elemento dichiarato in questo schema o un altro schema indicato dallo spazio dei nomi specificato.

SchemaType

Ottiene o imposta il tipo dell'elemento. Può trattarsi di un tipo complesso o di un tipo semplice.

SchemaTypeName

Ottiene o imposta il nome di un tipo di dati predefinito definito in questo schema o in un altro schema indicato dallo spazio dei nomi specificato.

SourceUri

Ottiene o imposta il percorso di origine per il file che ha caricato lo schema.

(Ereditato da XmlSchemaObject)
SubstitutionGroup

Ottiene o imposta il nome di un elemento sostituito da questo elemento.

UnhandledAttributes

Ottiene o imposta gli attributi qualificati che non appartengono allo spazio dei nomi di destinazione dello schema corrente.

(Ereditato da XmlSchemaAnnotated)

Metodi

Nome Descrizione
Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a