XmlSchemaSet XmlSchemaSet XmlSchemaSet XmlSchemaSet Class

定義

包含 XML 結構描述定義語言 (XSD) 結構描述的快取。Contains a cache of XML Schema definition language (XSD) schemas.

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

範例

下列範例會使用儲存在 XmlSchemaSet 中的結構描述,來驗證 XML 檔案。The following example validates an XML file using schemas stored in the XmlSchemaSet. XML 檔案中的命名空間 (urn:bookstore-schema),會識別 XmlSchemaSet 中用於驗證的結構描述。The namespace in the XML file, urn:bookstore-schema, identifies which schema in the XmlSchemaSet to use for validation. 此範例的輸出顯示 XML 檔案有兩個架構違規:Output from the example shows that the XML file has two schema violations:

  • 第一<本書 > 元素<包含作者 > 元素, 但沒有<標題 > 或<價格 > 專案。The first <book> element contains an <author> element but no <title> or <price> element.

  • 最後<一< <本書 > 元素中的作者 > 專案遺漏名字>和姓氏>元素,而改為名稱>元素無效。< <The <author> element in the last <book> element is missing a <first-name> and <last-name> element and instead has an invalid <name> element.

#using <System.Xml.dll>

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

// Display any validation errors.
static void ValidationCallBack( Object^ /*sender*/, ValidationEventArgs^ e )
{
   Console::WriteLine( L"Validation Error:\n   {0}", e->Message );
   Console::WriteLine();
}

int main()
{
   // Create the XmlSchemaSet class.
   XmlSchemaSet^ sc = gcnew XmlSchemaSet;

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

   // Set the validation settings.
   XmlReaderSettings^ settings = gcnew XmlReaderSettings;
   settings->ValidationType = ValidationType::Schema;
   settings->Schemas = sc;
   settings->ValidationEventHandler += gcnew ValidationEventHandler(ValidationCallBack);

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

   // Parse the file. 
   while ( reader->Read() )
      ;

   return 1;
}
// 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'.
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

範例會使用下列兩個輸入檔案。The sample uses the following two input files.

booksSchemaFail.xml: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>

書籍 .xsd: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>

備註

重要

  • 請勿使用來自未知或不受信任來源或位置的架構。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.
  • 「安全性考慮」一節中涵蓋了其他安全性考慮。Additional security considerations are covered in the "Security Considerations" section.

XmlSchemaSet是您可以儲存 XML 架構定義語言 (XSD) 架構的快取或程式庫。XmlSchemaSet is a cache or library where you can store XML Schema definition language (XSD) schemas. XmlSchemaSet 會藉由在記憶體中快取結構描述,而不是從檔案或 URL 來存取它們,來增加效能。XmlSchemaSet improves performance by caching schemas in memory instead of accessing them from a file or a URL. 每個架構都是由命名空間 URI 和將架構加入至集合時所指定的位置所識別。Each schema is identified by the namespace URI and location that was specified when the schema was added to the set. 您可以使用XmlReaderSettings.Schemas屬性來XmlSchemaSet指派 XML 讀取器應該用來進行資料驗證的物件。You use the XmlReaderSettings.Schemas property to assign the XmlSchemaSet object an XML reader should use for data validation.

安全性考量Security Considerations

  • 請勿使用來自未知或不受信任來源的架構。Do not use schemas from unknown or untrusted sources. 這麼做會危害程式碼的安全性。Doing so will compromise the security of your code. 架構之 include、import 和 redefine 元素中所參考的外部命名空間或位置, 會與包含或匯入它們之架構的基底 URI 相關解析。External namespaces or locations referenced in include, import, and redefine elements of a schema are resolved with respect to the base URI of the schema that includes or imports them. 例如, 如果包含或匯入架構的基底 URI 是空的或null, 則會根據目前目錄來解析外部位置。For example, if the base URI of the including or importing schema is empty or null, the external locations are resolved with respect to the current directory. 根據預設, 會使用類別來解析外部架構。XmlUrlResolverThe XmlUrlResolver class is used to resolve external schemas by default. 若要停用架構之 include、import 和 redefine 元素的解析, 請將XmlSchemaSet.XmlResolver屬性設定null為。To disable resolution of include, import, and redefine elements of a schema, set the XmlSchemaSet.XmlResolver property to null.

  • XmlSchemaSet類別會System.Text.RegularExpressions.Regex使用類別來剖析和比對 XML 架構中的正則運算式。The XmlSchemaSet class uses the System.Text.RegularExpressions.Regex class to parse and match regular expressions in an XML schema. 在 XML 架構中使用正則運算式來驗證模式 facet 可能會涉及增加的 CPU 使用量, 而且在高可用性案例中應該予以避免。Validation of pattern facets with regular expressions in an XML schema may involve increased CPU usage and should be avoided in high availability scenarios.

  • 因使用XmlSchemaSet類別而引發的例外狀況, 例如XmlSchemaException類別可能包含不應該在未受信任的案例中公開的機密資訊。Exceptions raised as a result of using the XmlSchemaSet class, such as the XmlSchemaException class may contain sensitive information that should not be exposed in untrusted scenarios. 例如, SourceUri的屬性XmlSchemaException會將 URI 路徑傳回造成例外狀況的架構檔案。For example, the SourceUri property of an XmlSchemaException returns the URI path to the schema file that caused the exception. 屬性SourceUri不應該在未受信任的案例中公開。The SourceUri property should not be exposed in untrusted scenarios. 應該適當地處理例外狀況, 如此一來, 就不會在未受信任的案例中公開此機密資訊。Exceptions should be properly handled so that this sensitive information is not exposed in untrusted scenarios.

建構函式

XmlSchemaSet() XmlSchemaSet() XmlSchemaSet() XmlSchemaSet()

初始化 XmlSchemaSet 類別的新執行個體。Initializes a new instance of the XmlSchemaSet class.

XmlSchemaSet(XmlNameTable) XmlSchemaSet(XmlNameTable) XmlSchemaSet(XmlNameTable) XmlSchemaSet(XmlNameTable)

使用指定的 XmlSchemaSet 初始化 XmlNameTable 類別的新執行個體。Initializes a new instance of the XmlSchemaSet class with the specified XmlNameTable.

屬性

CompilationSettings CompilationSettings CompilationSettings CompilationSettings

取得或設定 XmlSchemaCompilationSettingsXmlSchemaSetGets or sets the XmlSchemaCompilationSettings for the XmlSchemaSet.

Count Count Count Count

取得 XmlSchemaSet 中的邏輯 XML 結構描述定義語言 (XSD) 結構描述數目。Gets the number of logical XML Schema definition language (XSD) schemas in the XmlSchemaSet.

GlobalAttributes GlobalAttributes GlobalAttributes GlobalAttributes

取得 XmlSchemaSet 中所有 XML 結構描述定義語言 (XSD) 結構描述的所有全域屬性。Gets all the global attributes in all the XML Schema definition language (XSD) schemas in the XmlSchemaSet.

GlobalElements GlobalElements GlobalElements GlobalElements

取得 XmlSchemaSet 中所有 XML 結構描述定義語言 (XSD) 結構描述的所有全域項目。Gets all the global elements in all the XML Schema definition language (XSD) schemas in the XmlSchemaSet.

GlobalTypes GlobalTypes GlobalTypes GlobalTypes

取得 XmlSchemaSet 中所有 XML 結構描述定義語言 (XSD) 結構描述的所有全域簡單和複雜型別。Gets all of the global simple and complex types in all the XML Schema definition language (XSD) schemas in the XmlSchemaSet.

IsCompiled IsCompiled IsCompiled IsCompiled

取得值,指出是否已編譯 XmlSchemaSet 中的 XML 結構描述定義語言 (XSD) 結構描述。Gets a value that indicates whether the XML Schema definition language (XSD) schemas in the XmlSchemaSet have been compiled.

NameTable NameTable NameTable NameTable

當載入新的 XML 結構描述定義語言 (XSD) 結構描述時,取得 XmlNameTable 所使用的預設 XmlSchemaSetGets the default XmlNameTable used by the XmlSchemaSet when loading new XML Schema definition language (XSD) schemas.

XmlResolver XmlResolver XmlResolver XmlResolver

設定 XmlResolver,用於解析在結構描述之 Include 和 Import 項目中參考的命名空間或位置。Sets the XmlResolver used to resolve namespaces or locations referenced in include and import elements of a schema.

方法

Add(String, String) Add(String, String) Add(String, String) Add(String, String)

將指定 URL 處的 XML 結構描述定義語言 (XSD) 結構描述加入 XmlSchemaSetAdds the XML Schema definition language (XSD) schema at the URL specified to the XmlSchemaSet.

Add(String, XmlReader) Add(String, XmlReader) Add(String, XmlReader) Add(String, XmlReader)

XmlReader 中包含的 XML 結構描述定義語言 (XSD) 結構描述加入 XmlSchemaSetAdds the XML Schema definition language (XSD) schema contained in the XmlReader to the XmlSchemaSet.

Add(XmlSchema) Add(XmlSchema) Add(XmlSchema) Add(XmlSchema)

將指定的 XmlSchema 加入 XmlSchemaSetAdds the given XmlSchema to the XmlSchemaSet.

Add(XmlSchemaSet) Add(XmlSchemaSet) Add(XmlSchemaSet) Add(XmlSchemaSet)

將指定 XmlSchemaSet 中的所有 XML 結構描述定義語言 (XSD) 結構描述加入 XmlSchemaSetAdds all the XML Schema definition language (XSD) schemas in the given XmlSchemaSet to the XmlSchemaSet.

Compile() Compile() Compile() Compile()

將加入至 XmlSchemaSet 的 XML 結構描述定義語言 (XSD) 結構描述,編譯為一個邏輯結構描述。Compiles the XML Schema definition language (XSD) schemas added to the XmlSchemaSet into one logical schema.

Contains(String) Contains(String) Contains(String) Contains(String)

指出具有指定目標命名空間 URI 的 XML 結構描述定義語言 (XSD) 結構描述是否位於 XmlSchemaSet 中。Indicates whether an XML Schema definition language (XSD) schema with the specified target namespace URI is in the XmlSchemaSet.

Contains(XmlSchema) Contains(XmlSchema) Contains(XmlSchema) Contains(XmlSchema)

指出指定的 XML 結構描述定義語言 (XSD) XmlSchema 物件是否位於 XmlSchemaSet 中。Indicates whether the specified XML Schema definition language (XSD) XmlSchema object is in the XmlSchemaSet.

CopyTo(XmlSchema[], Int32) CopyTo(XmlSchema[], Int32) CopyTo(XmlSchema[], Int32) CopyTo(XmlSchema[], Int32)

複製 XmlSchema 中的所有 XmlSchemaSet 物件至指定陣列,開始於指定的索引處。Copies all the XmlSchema objects from the XmlSchemaSet to the given array, starting at the given index.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(XmlSchema) Remove(XmlSchema) Remove(XmlSchema) Remove(XmlSchema)

移除 XmlSchemaSet 中的指定 XML 結構描述定義語言 (XSD) 結構描述。Removes the specified XML Schema definition language (XSD) schema from the XmlSchemaSet.

RemoveRecursive(XmlSchema) RemoveRecursive(XmlSchema) RemoveRecursive(XmlSchema) RemoveRecursive(XmlSchema)

移除 XmlSchemaSet 中指定的 XML 結構描述定義語言 (XSD) 結構描述及其匯入的所有結構描述。Removes the specified XML Schema definition language (XSD) schema and all the schemas it imports from the XmlSchemaSet.

Reprocess(XmlSchema) Reprocess(XmlSchema) Reprocess(XmlSchema) Reprocess(XmlSchema)

重新處理已存在於 XmlSchemaSet 中的 XML 結構描述定義語言 (XSD) 結構描述。Reprocesses an XML Schema definition language (XSD) schema that already exists in the XmlSchemaSet.

Schemas() Schemas() Schemas() Schemas()

傳回 XmlSchemaSet 中所有 XML 結構描述定義語言 (XSD) 結構描述的集合。Returns a collection of all the XML Schema definition language (XSD) schemas in the XmlSchemaSet.

Schemas(String) Schemas(String) Schemas(String) Schemas(String)

傳回 XmlSchemaSet 中屬於指定命名空間之所有 XML 結構描述定義語言 (XSD) 結構描述的集合。Returns a collection of all the XML Schema definition language (XSD) schemas in the XmlSchemaSet that belong to the given namespace.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

事件

ValidationEventHandler ValidationEventHandler ValidationEventHandler ValidationEventHandler

指定事件處理常式,接收 XML 結構描述定義語言 (XSD) 結構描述驗證錯誤的資訊。Specifies an event handler for receiving information about XML Schema definition language (XSD) schema validation errors.

適用於