XmlSchemaElement 類別

定義

代表 element 由萬維網聯盟(W3C)規定的 XML 結構元素。 此類別是所有粒子類型的基底類別,用於描述 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
繼承

範例

以下範例會產生該 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

以下 XML 檔案用於上述程式碼範例。

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

備註

Important

  • 請勿使用來自未知或未受信任來源或位置的架構。 這樣做會危害程式代碼的安全性。
  • XML 架構(包括內嵌架構)原本就容易受到阻斷服務攻擊;在未受信任的案例中不接受它們。
  • 架構驗證錯誤訊息和例外狀況可能會公開架構檔案內容模型或 URI 路徑的敏感性資訊。 請小心不要將此資訊公開給不受信任的來電者。

建構函式

名稱 Description
XmlSchemaElement()

初始化 XmlSchemaElement 類別的新執行個體。

屬性

名稱 Description
Annotation

取得或設定該 annotation 屬性。

(繼承來源 XmlSchemaAnnotated)
Block

取得或設定導 Block 出。

BlockResolved

取得該物業編譯後的價值 Block

Constraints

取得元素上的約束集合。

DefaultValue

若元素內容為簡單型態,則 textOnly取得或設定該元素的預設值。

ElementSchemaType

根據元素的值XmlSchemaType,取得SchemaType一個代表元素SchemaTypeName型態的物件。

ElementType
已淘汰.
已淘汰.
已淘汰.

取得基於XmlSchemaElementXmlSchemaElement元素或的通用語言執行時(CLR)物件,該物件包含該屬性編譯後的值ElementType

Final

取得或設定 Final 屬性,表示不允許進一步推導。

FinalResolved

取得該物業編譯後的價值 Final

FixedValue

取得或設定固定值。

Form

取得或設定元素的形式。

Id

取得或設定字串 ID。

(繼承來源 XmlSchemaAnnotated)
IsAbstract

取得或設定資訊,指示該元素是否能用於實例文件。

IsNillable

取得或設定資訊,指示是否 xsi:nil 可能出現在實例資料中。 表示是否可以為該元素賦予明確的 nil 值。

LineNumber

取得或設定該 schema 元素所指檔案中的行號。

(繼承來源 XmlSchemaObject)
LinePosition

取得或設定該元素所指檔案 schema 中的行位。

(繼承來源 XmlSchemaObject)
MaxOccurs

取得或設定粒子出現的最大次數。

(繼承來源 XmlSchemaParticle)
MaxOccursString

取得或設定該數字為字串值。 粒子最多能出現的次數。

(繼承來源 XmlSchemaParticle)
MinOccurs

取得或設定該粒子出現的最少次數。

(繼承來源 XmlSchemaParticle)
MinOccursString

取得或設定該數字為字串值。 粒子能出現的最少次數。

(繼承來源 XmlSchemaParticle)
Name

取得或設定元素名稱。

Namespaces

用這個結構物件取得或設定 XmlSerializerNamespaces 使用 。

(繼承來源 XmlSchemaObject)
Parent

取得或設定此 XmlSchemaObject的父節點。

(繼承來源 XmlSchemaObject)
QualifiedName

取得該元素的實際合格名稱。

RefName

取得或設定此結構中宣告的元素(或指定命名空間中其他結構)的參考名稱。

SchemaType

取得或設定元素的型別。 這可以是複雜型態或簡單型態。

SchemaTypeName

取得或設定此結構中定義的內建資料型別名稱,或指定命名空間中指示的其他結構。

SourceUri

取得或設定載入結構檔案的來源位置。

(繼承來源 XmlSchemaObject)
SubstitutionGroup

取得或設定一個被替換為該元素的名稱。

UnhandledAttributes

取得或設定不屬於目前結構目標命名空間的限定屬性。

(繼承來源 XmlSchemaAnnotated)

方法

名稱 Description
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於