XmlSchema 類別

定義

依全球資訊網協會 (W3C) 的 XML 結構描述第 1 部分:結構 (英文) 及 XML 結構描述第 2 部分:資料類型 (英文) 所規定之 XML 結構描述的記憶體內部表示。

public ref class XmlSchema
public ref class XmlSchema : System::Xml::Schema::XmlSchemaObject
public class XmlSchema
public class XmlSchema : System.Xml.Schema.XmlSchemaObject
type XmlSchema = class
type XmlSchema = class
    inherit XmlSchemaObject
Public Class XmlSchema
Public Class XmlSchema
Inherits XmlSchemaObject
繼承
XmlSchema
繼承

範例

下列範例會建立架構定義。

#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="xs: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="xs: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="xs: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="xs: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);
    }
}
Option Explicit On
Option Strict On

Imports System.Xml
Imports System.Xml.Schema

Class XMLSchemaExamples
    Public Shared Sub Main()
        Dim schema As New XmlSchema()

        ' <xs:element name="cat" type="xs: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="xs: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" type="xs:string" substitutionGroup="dog"/>
    <xs:element name="brownDog" type="xs:string" 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>

備註

重要

  • 請勿使用未知或不受信任的來源或位置的架構。 這樣做會危害程式碼的安全性。
  • XML 架構 (包括內嵌架構) 原本就容易遭受阻斷服務攻擊;在不受信任的案例中不接受它們。
  • 使用 XmlSchema 類別之類的 XmlSchemaException 類別所引發的例外狀況可能包含不應在不受信任的案例中公開的敏感性資訊。 例如,的 XmlSchemaException 屬性會 SourceUri 傳回造成例外狀況之架構檔案的 URI 路徑。 屬性 SourceUri 不應該在不受信任的案例中公開。 例外狀況應該適當地處理,因此此敏感性資訊不會在不受信任的案例中公開。

建構函式

XmlSchema()

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

欄位

InstanceNamespace

XML 結構描述執行個體命名空間。 這個欄位為常數。

Namespace

XML 結構描述命名空間。 這個欄位為常數。

屬性

AttributeFormDefault

取得或設定在結構描述目標命名空間中宣告的屬性 (Attribute) 格式。

AttributeGroups

取得結構描述中所有全域屬性群組的後結構描述編譯值。

Attributes

取得結構描述中所有屬性的後結構描述編譯值。

BlockDefault

取得或設定 blockDefault 屬性,該屬性設定結構描述之 block 中項目和複雜型別上 targetNamespace 屬性的預設值。

ElementFormDefault

取得或設定在結構描述目標命名空間中宣告的元素格式。

Elements

取得結構描述中所有元素的後結構描述編譯值。

FinalDefault

取得或設定 finalDefault 屬性,該屬性設定結構描述之目標命名空間中項目和複雜型別上 final 屬性的預設值。

Groups

取得結構描述中所有群組的後結構描述編譯值。

Id

取得或設定字串 ID。

Includes

取得包含和匯入結構描述的集合。

IsCompiled

指出結構描述是否已編譯。

Items

取得結構描述中結構描述項目的集合,並用於在 schema 項目層級加入新項目型別。

LineNumber

取得或設定 schema 項目參考之檔案中的行號。

(繼承來源 XmlSchemaObject)
LinePosition

取得或設定 schema 項目參考之檔案中的行位置。

(繼承來源 XmlSchemaObject)
Namespaces

取得或設定 XmlSerializerNamespaces,以便與這個結構描述物件一起使用。

(繼承來源 XmlSchemaObject)
Notations

取得結構描述中所有標記法的後結構描述編譯值。

Parent

取得或設定這個 XmlSchemaObject 的父項。

(繼承來源 XmlSchemaObject)
SchemaTypes

取得結構描述中所有結構描述類型的後結構描述編譯值。

SourceUri

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

(繼承來源 XmlSchemaObject)
TargetNamespace

取得或設定結構描述目標命名空間的統一資源識別元 (URI)。

UnhandledAttributes

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

Version

取得或設定結構描述的版本。

方法

Compile(ValidationEventHandler)
已過時。
已過時。
已過時。

將 XML 結構描述物件模型 (SOM) 編譯成驗證用的結構描述資訊。 用於檢查以程式方式建置的 SOM 的語法和語意結構。 語意驗證檢查是在編譯階段執行。

Compile(ValidationEventHandler, XmlResolver)
已過時。
已過時。
已過時。

將 XML 結構描述物件模型 (SOM) 編譯成驗證用的結構描述資訊。 用於檢查以程式方式建置的 SOM 的語法和語意結構。 語意驗證檢查是在編譯階段執行。

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Read(Stream, ValidationEventHandler)

從提供的資料流程讀取 XML 架構。

Read(TextReader, ValidationEventHandler)

從提供的 TextReader 讀取 XML 結構描述。

Read(XmlReader, ValidationEventHandler)

從提供的 XmlReader 讀取 XML 結構描述。

ToString()

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

(繼承來源 Object)
Write(Stream)

將 XML 結構描述寫入提供的資料流。

Write(Stream, XmlNamespaceManager)

使用指定的 Stream,將 XML 結構描述寫入提供的 XmlNamespaceManager

Write(TextWriter)

將 XML 結構描述寫入提供的 TextWriter

Write(TextWriter, XmlNamespaceManager)

將 XML 結構描述寫入提供的 TextWriter

Write(XmlWriter)

將 XML 結構描述寫入提供的 XmlWriter

Write(XmlWriter, XmlNamespaceManager)

將 XML 結構描述寫入提供的 XmlWriter

適用於

另請參閱