XmlSchemaElement Clase

Definición

Representa el element elemento del esquema XML tal y como especifica World Wide Web Consortium (W3C). Esta clase es la clase base para todos los tipos de partículas y se usa para describir un elemento en 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
Herencia

Ejemplos

En el ejemplo siguiente se crea el element elemento .

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

El siguiente archivo XML se usa para el ejemplo de código anterior.

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

Comentarios

Importante

  • No use esquemas de orígenes o ubicaciones desconocidos o que no sean de confianza. Si lo hace, se pondrá en peligro la seguridad del código.
  • Los esquemas XML (incluidos los esquemas insertados) son inherentemente vulnerables a ataques de denegación de servicio; no los acepte en escenarios que no son de confianza.
  • Los mensajes de error y excepciones de validación de esquemas pueden exponer información confidencial sobre el modelo de contenido o las rutas de acceso de URI al archivo de esquema. Tenga cuidado de no exponer esta información a los autores de llamadas que no son de confianza.

Constructores

Nombre Description
XmlSchemaElement()

Inicializa una nueva instancia de la clase XmlSchemaElement.

Propiedades

Nombre Description
Annotation

Obtiene o establece la annotation propiedad .

(Heredado de XmlSchemaAnnotated)
Block

Obtiene o establece una Block derivación.

BlockResolved

Obtiene el valor posterior a la compilación de la Block propiedad .

Constraints

Obtiene la colección de restricciones en el elemento .

DefaultValue

Obtiene o establece el valor predeterminado del elemento si su contenido es un tipo simple o contenido del elemento es textOnly.

ElementSchemaType

Obtiene un XmlSchemaType objeto que representa el tipo del elemento en función de los SchemaType valores o SchemaTypeName del elemento.

ElementType
Obsoletos.
Obsoletos.
Obsoletos.

Obtiene un objeto de Common Language Runtime (CLR) basado en o XmlSchemaElementXmlSchemaElement en el elemento , que contiene el valor posterior a la compilación de la ElementType propiedad .

Final

Obtiene o establece la Final propiedad para indicar que no se permiten derivaciones adicionales.

FinalResolved

Obtiene el valor posterior a la compilación de la Final propiedad .

FixedValue

Obtiene o establece el valor fijo.

Form

Obtiene o establece el formulario para el elemento .

Id

Obtiene o establece el identificador de cadena.

(Heredado de XmlSchemaAnnotated)
IsAbstract

Obtiene o establece información para indicar si el elemento se puede usar en un documento de instancia.

IsNillable

Obtiene o establece información que indica si xsi:nil se puede producir en los datos de instancia. Indica si se puede asignar un valor nulo explícito al elemento .

LineNumber

Obtiene o establece el número de línea del archivo al que hace referencia el schema elemento.

(Heredado de XmlSchemaObject)
LinePosition

Obtiene o establece la posición de línea del archivo al que hace referencia el schema elemento.

(Heredado de XmlSchemaObject)
MaxOccurs

Obtiene o establece el número máximo de veces que puede producirse la partícula.

(Heredado de XmlSchemaParticle)
MaxOccursString

Obtiene o establece el número como un valor de cadena. Número máximo de veces que se puede producir la partícula.

(Heredado de XmlSchemaParticle)
MinOccurs

Obtiene o establece el número mínimo de veces que puede producirse la partícula.

(Heredado de XmlSchemaParticle)
MinOccursString

Obtiene o establece el número como un valor de cadena. Número mínimo de veces que puede producirse la partícula.

(Heredado de XmlSchemaParticle)
Name

Obtiene o establece el nombre del elemento.

Namespaces

Obtiene o establece el objeto XmlSerializerNamespaces que se va a usar con este objeto de esquema.

(Heredado de XmlSchemaObject)
Parent

Obtiene o establece el elemento primario de este XmlSchemaObjectobjeto .

(Heredado de XmlSchemaObject)
QualifiedName

Obtiene el nombre completo real del elemento especificado.

RefName

Obtiene o establece el nombre de referencia de un elemento declarado en este esquema (u otro esquema indicado por el espacio de nombres especificado).

SchemaType

Obtiene o establece el tipo del elemento. Puede ser un tipo complejo o un tipo simple.

SchemaTypeName

Obtiene o establece el nombre de un tipo de datos integrado definido en este esquema u otro esquema indicado por el espacio de nombres especificado.

SourceUri

Obtiene o establece la ubicación de origen del archivo que cargó el esquema.

(Heredado de XmlSchemaObject)
SubstitutionGroup

Obtiene o establece el nombre de un elemento que se sustituye por este elemento.

UnhandledAttributes

Obtiene o establece los atributos calificados que no pertenecen al espacio de nombres de destino del esquema actual.

(Heredado de XmlSchemaAnnotated)

Métodos

Nombre Description
Equals(Object)

Determina si el objeto especificado es igual al objeto actual.

(Heredado de Object)
GetHashCode()

Actúa como función hash predeterminada.

(Heredado de Object)
GetType()

Obtiene el Type de la instancia actual.

(Heredado de Object)
MemberwiseClone()

Crea una copia superficial del Objectactual.

(Heredado de Object)
ToString()

Devuelve una cadena que representa el objeto actual.

(Heredado de Object)

Se aplica a