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:

  • 第一<本书 > 元素包含一个<作者 > 元素, 但<没有标题 > <或 price > 元素。The first <book> element contains an <author> element but no <title> or <price> element.

  • 上周< < >元素中的<author > 元素缺少名称>和姓氏>元素,而名称>元素无效。< <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. 在架构的包含、导入和重新定义元素中引用的外部命名空间或位置是根据包含或导入这些元素的架构的基本 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、import 和 redefine 元素禁用解析, 请将XmlSchemaSet.XmlResolver属性设置为。 nullTo disable resolution of include, import, and redefine elements of a schema, set the XmlSchemaSet.XmlResolver property to null.

  • XmlSchemaSetSystem.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.

适用于