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.

#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: {0}", e->Message );
}

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;
}
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 += new 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: {0}", e.Message);
  }
}
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO

public class Sample 

  public shared 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 shared sub ValidationCallBack(sender as object, e as ValidationEventArgs) 
    Console.WriteLine("Validation Error: {0}", e.Message)
  end sub
end class

輸入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>

books.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>

輸出Output

驗證錯誤:命名空間 urn:bookstore-schema 中的項目 book 在 urn:bookstore-schema 命名空間中具有無效的項目子系 author。Validation Error: The element 'book' in namespace 'urn:bookstore-schema' has invalid child element 'author' in namespace 'urn:bookstore-schema'. 命名空間 urn:bookstore-schema 中預期的是 title。Expected 'title' in namespace 'urn:bookstore-schema'.

驗證錯誤:命名空間 urn:bookstore-schema 中的項目 author 在 urn:bookstore-schema 命名空間中具有無效的項目子系 'name'。Validation Error: The element 'author' in namespace 'urn:bookstore-schema' has invalid child element 'name' in namespace 'urn:bookstore-schema'. 命名空間 urn:bookstore-schema 中預期的是 first-name。Expected 'first-name' in namespace 'urn:bookstore-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. 請小心,不要公開 (expose) 給未受信任的呼叫端的這項資訊。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. 外部命名空間或位置中參考包含,匯入和重新定義結構描述項目的解決相關的結構描述併入或匯入它們的基底 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. XmlUrlResolver類別預設用來解析外部結構描述。The XmlUrlResolver class is used to resolve external schemas by default. 若要停用解析的 include,匯入,並重新定義結構描述的項目,設定XmlSchemaSet.XmlResolver屬性設nullTo 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

取得或設定 XmlSchemaCompilationSettingsXmlSchemaSet Gets 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 所使用的預設 XmlSchemaSet Gets 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) 結構描述加入 XmlSchemaSet Adds 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) 結構描述加入 XmlSchemaSet Adds the XML Schema definition language (XSD) schema contained in the XmlReader to the XmlSchemaSet.

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

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

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

將指定 XmlSchemaSet 中的所有 XML 結構描述定義語言 (XSD) 結構描述加入 XmlSchemaSet Adds 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()

取得目前執行個體的 Type Gets 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.

適用於