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>

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>

注解

重要

  • 不要使用来自未知或不受信任的源或位置的架构。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. 外部命名空间或位置中引用包含,导入和重新定义元素的架构进行解析的基 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. 若要禁用解析导入、 包括和重新定义架构的元素,设置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 架构中的正则表达式模式方面的验证可能涉及到更高的 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

设置用于解析在架构的包含和导入元素中引用的命名空间或位置的 XmlResolverSets 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) 架构是否位于 XmlSchemaSetIndicates 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 对象是否位于 XmlSchemaSetIndicates 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 的浅表副本。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.

适用于