語言

XmlSchemaSet 類別

定義

包含 XML Schema 定義語言(XSD)架構的快取。

public ref class XmlSchemaSet
public class XmlSchemaSet
type XmlSchemaSet = class
Public Class XmlSchemaSet
繼承
XmlSchemaSet

範例

下列範例會使用 儲存在 中的 XmlSchemaSet架構來驗證 XML 檔案。 XML 檔案中的命名空間urn:bookstore-schema,會識別出要用於驗證的架構位於XmlSchemaSet中。 範例的輸出顯示 XML 檔案有兩個架構違規:

  • 第一個 <book> 元素包含一個 <author> 元素,但沒有 <title> or <price> 元素。

  • <author>最後一個<book>元素缺少<first-name>和<last-name>元素,反而有一個無效的<name>元素。

using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;

public class Sample
{
  public static void Main() {

    // Create the XmlSchemaSet class.
    XmlSchemaSet sc = new XmlSchemaSet();

    // Add the schema to the collection.
    sc.Add("urn:bookstore-schema", "books.xsd");

    // Set the validation settings.
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.ValidationType = ValidationType.Schema;
    settings.Schemas = sc;
    settings.ValidationEventHandler += ValidationCallBack;

    // Create the XmlReader object.
    XmlReader reader = XmlReader.Create("booksSchemaFail.xml", settings);

    // Parse the file.
    while (reader.Read());
  }

  // Display any validation errors.
  private static void ValidationCallBack(object sender, ValidationEventArgs e) {
    Console.WriteLine($"Validation Error:\n   {e.Message}\n");
  }
}
// The example displays output like the following:
//   Validation Error:
//        The element 'book' in namespace 'urn:bookstore-schema' has invalid child element 'author'
//        in namespace 'urn:bookstore-schema'. List of possible elements expected: 'title' in
//        namespace 'urn:bookstore-schema'.
//
//    Validation Error:
//       The element 'author' in namespace 'urn:bookstore-schema' has invalid child element 'name'
//       in namespace 'urn:bookstore-schema'. List of possible elements expected: 'first-name' in
//       namespace 'urn:bookstore-schema'.
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO

Public Module Sample 
  Public Sub Main() 

    ' Create the XmlSchemaSet class.
    Dim sc as XmlSchemaSet = new XmlSchemaSet()

    ' Add the schema to the collection.
    sc.Add("urn:bookstore-schema", "books.xsd")

    ' Set the validation settings.
    Dim settings as XmlReaderSettings = new XmlReaderSettings()
    settings.ValidationType = ValidationType.Schema
    settings.Schemas = sc
    AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack
 
    ' Create the XmlReader object.
    Dim reader as XmlReader = XmlReader.Create("booksSchemaFail.xml", settings)

    ' Parse the file. 
    While reader.Read()
    End While
    
  End Sub

  ' Display any validation errors.
  Private Sub ValidationCallBack(sender as object, e as ValidationEventArgs) 
    Console.WriteLine($"Validation Error:{vbCrLf}   {e.Message}")
    Console.WriteLine()
  End Sub
End Module
' The example displays output like the following:
'   Validation Error: 
'        The element 'book' in namespace 'urn:bookstore-schema' has invalid child element 'author' 
'        in namespace 'urn:bookstore-schema'. List of possible elements expected: 'title' in 
'        namespace 'urn:bookstore-schema'.
'
'    Validation Error: 
'       The element 'author' in namespace 'urn:bookstore-schema' has invalid child element 'name' 
'       in namespace 'urn:bookstore-schema'. List of possible elements expected: 'first-name' in 
'       namespace 'urn:bookstore-schema'.

Input

此範例會使用下列兩個輸入檔。

booksSchemaFail.xml:

<?xml version='1.0'?>
<bookstore xmlns="urn:bookstore-schema">
  <book>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
  </book>
  <book genre="novel">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

books.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="urn:bookstore-schema"
    elementFormDefault="qualified"
    targetNamespace="urn:bookstore-schema">

 <xsd:element name="bookstore" type="bookstoreType"/>

 <xsd:complexType name="bookstoreType">
  <xsd:sequence maxOccurs="unbounded">
   <xsd:element name="book"  type="bookType"/>
  </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="bookType">
  <xsd:sequence>
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="author" type="authorName"/>
   <xsd:element name="price"  type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="genre" type="xsd:string"/>
 </xsd:complexType>

 <xsd:complexType name="authorName">
  <xsd:sequence>
   <xsd:element name="first-name"  type="xsd:string"/>
   <xsd:element name="last-name" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>

</xsd:schema>

備註

Important

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

XmlSchemaSet 是快取或函式庫,您可以在其中儲存 XML 架構定義語言(XSD)綱要。 XmlSchemaSet 藉由將架構快取到記憶體中,而非從檔案或URL中存取,來改善效能。 每個架構都是透過將架構新增至集合時所指定的命名空間 URI 和位置來識別。 您可以使用 XmlReaderSettings.Schemas 屬性來指派 XmlSchemaSet XML 讀取器應該用於資料驗證的物件。

安全性考慮

  • 請勿使用來自未知或未受信任來源的架構。 這樣做會危害程式代碼的安全性。 在包含、匯入和重新定義架構元素中所參考的外部命名空間或位置,會根據包含或匯入這些架構的架構基底 URI 來解析。 例如,如果 包含或匯入架構的基底 URI 是空的 或 null,則會針對目前目錄解析外部位置。 類別 XmlUrlResolver 預設用來解析外部架構。 若要停用架構之 include、import 和 redefine 元素的解析,請將 屬性設定 XmlSchemaSet.XmlResolver 為 null。

  • 類別 XmlSchemaSet 使用類別 System.Text.RegularExpressions.Regex 來剖析和比對 XML 架構中的正則表示式。 在 XML 架構中,使用正規表達式進行模式 Facet 驗證可能會增加 CPU 使用量,因此在高可用性場景中應避免這樣做。

  • 因使用 XmlSchemaSet 類別而引發的例外狀況,例如 類別 XmlSchemaException 可能包含不應在不受信任的案例中公開的敏感性資訊。 例如, SourceUri 的 XmlSchemaException 屬性會傳回造成例外狀況之架構檔案的 URI 路徑。 屬性 SourceUri 不應在不受信任的情境中公開。 應正確處理例外狀況,如此一來,此敏感性資訊就不會在不受信任的案例中公開。

建構函式

名稱 Description
XmlSchemaSet()

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

XmlSchemaSet(XmlNameTable)

使用指定的 XmlSchemaSet,初始化 XmlNameTable 類別的新實例。

屬性

名稱 Description
CompilationSettings

取得或設定 XmlSchemaCompilationSettings 。XmlSchemaSet

Count

取得 . 中邏輯 XML 結構定義語言(XSD)結構 XmlSchemaSet的數量。

GlobalAttributes

取得所有 XML Schema 定義語言(XSD)架構 XmlSchemaSet中的所有全域屬性。

GlobalElements

取得所有 XML Schema 定義語言(XSD)架構中的所有全域元素。XmlSchemaSet

GlobalTypes

在 .中取得所有 XML Schema 定義語言(XSD)架構 XmlSchemaSet中的所有全域簡單與複雜型態。

IsCompiled

會取得一個值,表示 XML XmlSchemaSet Schema 定義語言(XSD)架構是否已被編譯完成。

NameTable

讀取新 XML Schema 定義語言(XSD)架構時所XmlNameTable使用的預設XmlSchemaSet值。

XmlResolver

用於解析結構中包含與匯入元素中引用的命名空間或位置的集合 XmlResolver 。

方法

名稱 Description
Add(String, String)

在指定的 URL 處新增 XML Schema 定義語言(XSD)架構。XmlSchemaSet

Add(String, XmlReader)

新增包含於 XmlReader 的 XML Schema 定義語言(XSD)架構。XmlSchemaSet

Add(XmlSchema)

將給出 XmlSchema 的 相加於 XmlSchemaSet。

Add(XmlSchemaSet)

新增所有 XML Schema 定義語言(XSD)架構 XmlSchemaSet 於 XmlSchemaSet。

Compile()

將新增 XmlSchemaSet 的 XML Schema 定義語言(XSD)架構編譯為一個邏輯架構。

Contains(String)

表示具有指定目標命名空間 URI 的 XML Schema 定義語言(XSD)架構是否屬於 XmlSchemaSet。

Contains(XmlSchema)

指示指定的 XML 結構定義語言(XSD) XmlSchema 物件是否屬於 XmlSchemaSet.

CopyTo(XmlSchema[], Int32)

從給定的索引開始,將所有 XmlSchema 物件 XmlSchemaSet 從 複製到給定陣列。

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type。

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
Remove(XmlSchema)

從 . 中移除指定的 XML 結構定義語言(XSD)架構。XmlSchemaSet

RemoveRecursive(XmlSchema)

移除指定的 XML 結構定義語言(XSD)架構及其從 中匯 XmlSchemaSet入的所有架構。

Reprocess(XmlSchema)

處理已存在 XmlSchemaSet於 .

Schemas()

回傳所有 XML Schema 定義語言(XSD)架構的集合。XmlSchemaSet

Schemas(String)

回傳屬於指定命名空間的所有 XML Schema 定義語言(XSD)架構 XmlSchemaSet 集合。

ToString()

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

(繼承來源 Object)

事件

名稱 Description
ValidationEventHandler

指定一個事件處理程序,用於接收有關 XML 結構定義語言(XSD)架構驗證錯誤的資訊。

適用於