XmlSchemaElement 类

定义

按 WWW 联合会 (W3C) 指定的方式显示 element 来自 XML 架构的元素。Represents the element element from XML Schema as specified by the World Wide Web Consortium (W3C). 此类是所有微粒类型的基类,用于描述 XML 文档中的元素。This class is the base class for all particle types and is used to describe an element in an XML document.

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
继承

示例

下面的示例创建 element 元素。The following example creates the element element.

#using <mscorlib.dll>
#using <System.Xml.dll>

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

class XmlSchemaExamples
{
public:

    static void Main()
    {
        XmlSchema^ schema = gcnew XmlSchema();

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

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

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

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

        // <xs:element name="pets">
        XmlSchemaElement^ elementPets = gcnew XmlSchemaElement();
        schema->Items->Add(elementPets);
        elementPets->Name = "pets";

        // <xs:complexType>
        XmlSchemaComplexType^ complexType = gcnew XmlSchemaComplexType();
        elementPets->SchemaType = complexType;

        // <xs:choice minOccurs="0" maxOccurs="unbounded">
        XmlSchemaChoice^ choice = gcnew XmlSchemaChoice();
        complexType->Particle = choice;
        choice->MinOccurs = 0;
        choice->MaxOccursString = "unbounded";

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

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

        XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
        schemaSet->ValidationEventHandler += gcnew ValidationEventHandler(ValidationCallbackOne);
        schemaSet->Add(schema);
        schemaSet->Compile();

        XmlSchema^ compiledSchema;

        for each (XmlSchema^ schema1 in schemaSet->Schemas())
        {
            compiledSchema = schema1;
        }

        XmlNamespaceManager^ nsmgr = gcnew XmlNamespaceManager(gcnew NameTable());
        nsmgr->AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
        compiledSchema->Write(Console::Out, nsmgr);
    }

    static void ValidationCallbackOne(Object^ sender, ValidationEventArgs^ args)
    {
        Console::WriteLine(args->Message);
    }
};

int main()
{
    XmlSchemaExamples::Main();
    return 0;
};
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

下面的 XML 文件用于前面的代码示例。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>

注解

重要

  • 不要使用来自未知或不受信任的源或位置的架构。Do not use schemas from unknown or untrusted sources or locations. 这样做会破坏代码的安全性。Doing so will compromise the security of your code.
  • XML 架构(包括内联架构)本质上容易受到拒绝服务攻击;不要在不受信任的情况下接受它们。XML schemas (including inline schemas) are inherently vulnerable to denial of service attacks; do not accept them in untrusted scenarios.
  • 架构验证错误消息和异常可能会向架构文件公开有关内容模型或 URI 路径的敏感信息。Schema validation error messages and exceptions may expose sensitive information about the content model or URI paths to the schema file. 请注意不要向不受信任的调用方公开此信息。Be careful not to expose this information to untrusted callers.

构造函数

XmlSchemaElement()

初始化 XmlSchemaElement 类的新实例。Initializes a new instance of the XmlSchemaElement class.

属性

Annotation

获取或设置 annotation 属性。Gets or sets the annotation property.

(继承自 XmlSchemaAnnotated)
Block

获取或设置 Block 派生。Gets or sets a Block derivation.

BlockResolved

获取 Block 属性的编译后值。Gets the post-compilation value of the Block property.

Constraints

获取对元素的约束的集合。Gets the collection of constraints on the element.

DefaultValue

如果元素的内容是简单类型,或者元素的内容为 textOnly,获取或设置元素的默认值。Gets or sets the default value of the element if its content is a simple type or content of the element is textOnly.

ElementSchemaType

获取一个 XmlSchemaType 对象,该对象表示元素基于其 SchemaTypeSchemaTypeName 值的类型。Gets an XmlSchemaType object representing the type of the element based on the SchemaType or SchemaTypeName values of the element.

ElementType

基于保存编译后 XmlSchemaElement 属性值的元素的 XmlSchemaElementElementType,获取公共语言运行时 (CLR) 对象。Gets a common language runtime (CLR) object based on the XmlSchemaElement or XmlSchemaElement of the element, which holds the post-compilation value of the ElementType property.

Final

获取或设置 Final 属性以指示不允许进一步派生。Gets or sets the Final property to indicate that no further derivations are allowed.

FinalResolved

获取 Final 属性的编译后值。Gets the post-compilation value of the Final property.

FixedValue

获取或设置固定值。Gets or sets the fixed value.

Form

获取或设置元素的窗体。Gets or sets the form for the element.

Id

获取或设置字符串 ID。Gets or sets the string id.

(继承自 XmlSchemaAnnotated)
IsAbstract

获取或设置信息以指示是否可在实例文档中使用该元素。Gets or sets information to indicate if the element can be used in an instance document.

IsNillable

获取或设置指示 xsi:nil 是否可以在实例数据中出现的信息。Gets or sets information that indicates if xsi:nil can occur in the instance data. 指示是否可以为元素赋予显式 Nil 值。Indicates if an explicit nil value can be assigned to the element.

LineNumber

获取或设置 schema 元素引用的文件中的行号。Gets or sets the line number in the file to which the schema element refers.

(继承自 XmlSchemaObject)
LinePosition

获取或设置 schema 元素引用的文件中的行位置。Gets or sets the line position in the file to which the schema element refers.

(继承自 XmlSchemaObject)
MaxOccurs

获取或设置粒子可发生的最大次数。Gets or sets the maximum number of times the particle can occur.

(继承自 XmlSchemaParticle)
MaxOccursString

获取设置字符串值形式的数字。Gets or sets the number as a string value. 粒子可发生的最大次数。Maximum number of times the particle can occur.

(继承自 XmlSchemaParticle)
MinOccurs

获取或设置粒子可发生的最小次数。Gets or sets the minimum number of times the particle can occur.

(继承自 XmlSchemaParticle)
MinOccursString

获取设置字符串值形式的数字。Gets or sets the number as a string value. 粒子可发生的最小次数。The minimum number of times the particle can occur.

(继承自 XmlSchemaParticle)
Name

获取或设置元素的名称。Gets or sets the name of the element.

Namespaces

获取或设置用于此架构对象的 XmlSerializerNamespacesGets or sets the XmlSerializerNamespaces to use with this schema object.

(继承自 XmlSchemaObject)
Parent

获取或设置此 XmlSchemaObject 的父级。Gets or sets the parent of this XmlSchemaObject.

(继承自 XmlSchemaObject)
QualifiedName

获取给定元素的实际限定名称。Gets the actual qualified name for the given element.

RefName

获取或设置在该架构(或指定的命名空间指示的另一个架构)中声明的元素的引用名称。Gets or sets the reference name of an element declared in this schema (or another schema indicated by the specified namespace).

SchemaType

获取或设置元素类型。Gets or sets the type of the element. 这可以是复杂类型,也可以是简单类型。This can either be a complex type or a simple type.

SchemaTypeName

获取或设置在该架构或由指定的命名空间指示的其他架构中定义的内置数据类型的名称。Gets or sets the name of a built-in data type defined in this schema or another schema indicated by the specified namespace.

SourceUri

获取或设置加载了架构的文件的源位置。Gets or sets the source location for the file that loaded the schema.

(继承自 XmlSchemaObject)
SubstitutionGroup

获取或设置由该元素替换的元素的名称。Gets or sets the name of an element that is being substituted by this element.

UnhandledAttributes

获取或设置不属于当前架构目标命名空间的限定特性。Gets or sets the qualified attributes that do not belong to the current schema's target namespace.

(继承自 XmlSchemaAnnotated)

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于