共用方式為


用於結構描述編譯的 XmlSchemaSet

說明 XmlSchemaSet,它是一種可儲存及驗證 XML 結構描述定義語言 (XSD) 結構描述的快取。

XmlSchemaSet 類別

XmlSchemaSet 是一種可儲存及驗證 XML 結構描述定義語言 (XSD) 結構描述的快取。

System.Xml 1.0 版中,已將 XML 結構描述載入 XmlSchemaCollection 類別,做為結構描述程式庫。在 System.Xml 2.0 版中,XmlValidatingReaderXmlSchemaCollection 類別已過時,並已分別由 Create 方法及 XmlSchemaSet 類別取代。

已經引入 XmlSchemaSet 以修正大量問題,包括標準相容性、效能和過時的 Microsoft XML 資料精簡 (XDR) 結構描述格式。

下列是 XmlSchemaCollection 類別與 XmlSchemaSet 類別的比較。

XmlSchemaCollection

XmlSchemaSet

支援 Microsoft XDR 和 W3C XML 結構描述。

只支援 W3C XML 結構描述。

當呼叫 Add 方法時,會編譯結構描述。

當呼叫 Add 方法時,不會編譯結構描述。這會改進結構描述程式庫建立期間的效能。

每個結構描述都會產生可導致「結構描述島」的個別編譯版本。因此,所有的 Include 和 Import 都只位於該結構描述範圍內。

編譯的結構描述會產生單一邏輯結構描述 (一「組」結構描述)。結構描述內已加入至該組的所有匯入結構描述,都會直接加入至它們自身的組。這表示全部型別都可供所有結構描述使用。

特定目標命名空間在集合中只可以有一個結構描述。

只要不存在型別衝突,就可以為相同的目標命名空間加入多個結構描述。

移轉至 XmlSchemaSet

下列程式碼範例提供從過時的 XmlSchemaCollection 類別到新 XmlSchemaSet 類別的移轉指南。該程式碼範例會說明兩種類別之間的下列主要差異。

下列是過時的 XmlSchemaCollection 程式碼範例。

Dim schemaCollection As XmlSchemaCollection = New XmlSchemaCollection()
schemaCollection.Add("https://www.contoso.com/retail", "https://www.contoso.com/retail.xsd")
schemaCollection.Add("https://www.contoso.com/books", "https://www.contoso.com/books.xsd")

Dim schema As XmlSchema

For Each schema in schemaCollection

   Console.WriteLine(schema.TargetNamespace)

Next
XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
schemaCollection.Add("https://www.contoso.com/retail", "https://www.contoso.com/retail.xsd");
schemaCollection.Add("https://www.contoso.com/books", "https://www.contoso.com/books.xsd");

foreach(XmlSchema schema in schemaCollection)
{
   Console.WriteLine(schema.TargetNamespace);
}

下列是對等的 XmlSchemaSet 程式碼範例。

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
schemaSet.Add("https://www.contoso.com/retail", "https://www.contoso.com/retail.xsd")
schemaSet.Add("https://www.contoso.com/books", "https://www.contoso.com/books.xsd")
schemaSet.Compile()

Dim schema As XmlSchema

For Each schema in schemaSet.Schemas()

   Console.WriteLine(schema.TargetNamespace)

Next
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add("https://www.contoso.com/retail", "https://www.contoso.com/retail.xsd");
schemaSet.Add("https://www.contoso.com/books", "https://www.contoso.com/books.xsd");
schemaSet.Compile();

foreach(XmlSchema schema in schemaSet.Schemas())
{
   Console.WriteLine(schema.TargetNamespace);
}

加入及擷取結構描述

使用 XmlSchemaSetAdd 方法,可將結構描述加入至 XmlSchemaSet。將結構描述加入至 XmlSchemaSet 後,便會與目標命名空間 URI 關聯。目標命名空間 URI 可指定為 Add 方法的參數,或如果未指定任何目標命名空間,則 XmlSchemaSet 會使用結構描述中定義的目標命名空間。

使用 XmlSchemaSetSchemas 屬性,可從 XmlSchemaSet 擷取結構描述。XmlSchemaSetSchemas 屬性可讓您重複處理 XmlSchemaSet 中包含的 XmlSchema 物件。Schemas 屬性會傳回 XmlSchemaSet 中包含的所有 XmlSchema 物件,或如果給定目標命名空間參數,則會傳回屬於目標命名空間的所有 XmlSchema 物件。如果將 null 指定為目標命名空間參數,則 Schemas 屬性會傳回所有結構描述但無命名空間。

下列範例會將 https://www.contoso.com/books 命名空間中的 books.xsd 結構描述加入至 XmlSchemaSet,從 XmlSchemaSet 擷取所有屬於 https://www.contoso.com/books 命名空間的結構描述,然後將那些結構描述寫入 Console

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet
schemaSet.Add("https://www.contoso.com/books", "books.xsd")

Dim schema As XmlSchema

For Each schema In schemaSet.Schemas("https://www.contoso.com/books")

   schema.Write(Console.Out)

Next
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add("https://www.contoso.com/books", "books.xsd");

foreach (XmlSchema schema in schemaSet.Schemas("https://www.contoso.com/books"))
{
   schema.Write(Console.Out);
}

如需在 XmlSchemaSet 物件中加入及擷取結構描述的詳細資訊,請參閱 Add 方法及 Schemas 屬性參考文件。

編譯結構描述

透過 XmlSchemaSetCompile 方法,可將 XmlSchemaSet 中的結構描述編譯成一個邏輯結構描述。

注意事項注意事項

不同於過時的 XmlSchemaCollection 類別,當呼叫 Add 方法時,不會編譯結構描述。

如果 Compile 方法執行成功,則 XmlSchemaSetIsCompiled 屬性會設為 true。

注意事項注意事項

如果結構描述是在 XmlSchemaSet 時編輯的,則 IsCompiled 屬性不受影響。也不會追蹤 XmlSchemaSet 中個別結構描述的更新。因此,即使 XmlSchemaSet 中的其中一個結構描述已變更,只要未在 XmlSchemaSet 中加入或移除結構描述,IsCompiled 屬性就可為 true。

下列範例會將 books.xsd 檔案加入至 XmlSchemaSet,然後呼叫 Compile 方法。

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
schemaSet.Add("https://www.contoso.com/books", "books.xsd")
schemaSet.Compile()
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add("https://www.contoso.com/books", "books.xsd");
schemaSet.Compile();

如需編譯 XmlSchemaSet 中結構描述的詳細資訊,請參閱 Compile 方法參考文件。

重新處理結構描述

當呼叫 XmlSchemaSetAdd 方法時,重新處理 XmlSchemaSet 中的結構描述會執行對結構描述執行的所有前置處理步驟。如果 Reprocess 方法呼叫成功,則 XmlSchemaSetIsCompiled 屬性會設為 false。

XmlSchemaSet 執行編譯之後,如果已修改 XmlSchemaSet 中的結構描述,則應使用 Reprocess 方法。

下列範例說明如何使用 Reprocess 方法重新處理加入至 XmlSchemaSet 的結構描述。在使用 Compile 方法編譯 XmlSchemaSet,並已修改加入至 XmlSchemaSet 的結構描述之後,IsCompiled 屬性會設為 true (即使已修改 XmlSchemaSet 中的結構描述)。呼叫 Reprocess 方法會執行 Add 方法所執行的所有前置處理,並將 IsCompiled 屬性設為 false。

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
Dim schema As XmlSchema = schemaSet.Add("https://www.contoso.com/books", "https://www.contoso.com/books.xsd")
schemaSet.Compile()

Dim element As XmlSchemaElement = New XmlSchemaElement()
schema.Items.Add(element)
element.Name = "book"
element.SchemaTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")

schemaSet.Reprocess(schema)
XmlSchemaSet schemaSet = new XmlSchemaSet();
XmlSchema schema = schemaSet.Add("https://www.contoso.com/books", "https://www.contoso.com/books.xsd");
schemaSet.Compile();

XmlSchemaElement element = new XmlSchemaElement();
schema.Items.Add(element);
element.Name = "book";
element.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

schemaSet.Reprocess(schema);

如需重新處理 XmlSchemaSet 中結構描述的詳細資訊,請參閱 Reprocess 方法參考文件。

檢查結構描述

您可以使用 XmlSchemaSetContains 方法,檢查結構描述是否包含在 XmlSchemaSet 中。Contains 方法允許檢查目標命名空間或 XmlSchema 物件。如果結構描述包含在 XmlSchemaSet 中,則在這兩種情況下,Contains 方法都會傳回 true;否則,傳回 false。

如需檢查結構描述的詳細資訊,請參閱 Contains 方法參考文件。

移除結構描述

使用 XmlSchemaSetRemoveRemoveRecursive 方法,可將結構描述從 XmlSchemaSet 中移除。Remove 方法會從 XmlSchemaSet 移除指定的結構描述,而 RemoveRecursive 方法會移除指定的結構描述及其從 XmlSchemaSet 匯入的所有結構描述。

下列範例說明如何將多個結構描述加入至 XmlSchemaSet,然後使用 RemoveRecursive 方法移除其中一個結構描述及其匯入的所有結構描述。

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
schemaSet.Add("https://www.contoso.com/retail", "https://www.contoso.com/retail.xsd")
schemaSet.Add("https://www.contoso.com/books", "https://www.contoso.com/books.xsd")
schemaSet.Add("https://www.contoso.com/music", "https://www.contoso.com/music.xsd")

Dim schema As XmlSchema

For Each schema In schemaSet.Schemas()

   If schema.TargetNamespace = "https://www.contoso.com/music" Then
      schemaSet.RemoveRecursive(schema)
   End If

Next
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add("https://www.contoso.com/retail", "https://www.contoso.com/retail.xsd");
schemaSet.Add("https://www.contoso.com/books", "https://www.contoso.com/books.xsd");
schemaSet.Add("https://www.contoso.com/music", "https://www.contoso.com/music.xsd");

foreach (XmlSchema schema in schemaSet.Schemas())
{
   if (schema.TargetNamespace == "https://www.contoso.com/music")
   {
      schemaSet.RemoveRecursive(schema);
   }
}

如需從 XmlSchemaSet 移除結構描述的詳細資訊,請參閱 RemoveRemoveRecursive 方法參考文件。

結構描述解析及 xs:import

下列範例說明當 XmlSchemaSet 中存在某個指定命名空間的多個結構描述時,匯入結構描述的 XmlSchemaSet 行為。

例如,設想一個 XmlSchemaSet,假設其中有 https://www.contoso.com 命名空間的多個結構描述。具有下列 xs:import 指示詞的結構描述會加入至 XmlSchemaSet

<xs:import namespace="https://www.contoso.com" schemaLocation="https://www.contoso.com/schema.xsd" />

XmlSchemaSet 會嘗試匯入 https://www.contoso.com 命名空間的結構描述,方法是從 https://www.contoso.com/schema.xsd URL 將其載入。即使在 XmlSchemaSet 中存在 https://www.contoso.com 命名空間的其他結構描述文件,也只有在結構描述文件中宣告的結構描述宣告及型別才可在匯入結構描述中使用。如果在 https://www.contoso.com/schema.xsd URL 處找不到 schema.xsd 檔案,則不會將 https://www.contoso.com 命名空間的任何結構描述匯入到匯入結構描述中。

驗證 XML 文件

您可根據 XmlSchemaSet 中的結構描述來驗證 XML 文件。驗證 XML 文件的方法是將結構描述加入至 XmlReaderSettings 物件的 XmlSchemaSetSchemas 屬性,或將 XmlSchemaSet 加入至 XmlReaderSettings 物件的 Schemas 屬性。然後,XmlReader 類別的 Create 方法會使用 XmlReaderSettings 物件來建立 XmlReader 物件並驗證 XML 文件。

如需使用 XmlSchemaSet 驗證 XML 文件的詳細資訊,請參閱 使用 XmlSchemaSet 驗證 XML 結構描述 (XSD)

請參閱

參考

Add

Schemas

Contains

Compile

Reprocess

Remove

RemoveRecursive

概念

用於結構描述編譯的 XmlSchemaSet

使用 XmlSchemaSet 驗證 XML 結構描述 (XSD)