XmlSchemaSet 类

定义

包含 XML 架构定义语言 (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> 元素或 <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 读取器应用于数据验证的对象。

安全注意事项

  • 不要使用来自未知或不受信任的源的架构。 这样做会损害代码的安全性。 架构的 include、import 和 redefine 元素中引用的外部命名空间或位置是根据包含或导入它们的架构的基本 URI 来解析的。 例如,如果包括或导入架构的基 URI 为空或 null,则外部位置将根据当前目录解析。 默认情况下,该 XmlUrlResolver 类用于解析外部架构。 若要禁用架构的包含、导入和重新定义元素的解析,请将 XmlSchemaSet.XmlResolver 属性设置为 null

  • XmlSchemaSet 类使用 System.Text.RegularExpressions.Regex 类来分析和匹配 XML 架构中的正则表达式。 在 XML 架构中使用正则表达式验证模式特征可能导致 CPU 使用率增加,因此在高可用性场景中应避免。

  • 由于使用 XmlSchemaSet 类而引发的异常,例如 XmlSchemaException 类,可能包含不应在不受信任的场景中公开的敏感信息。 例如,属性SourceUriXmlSchemaException返回导致异常的架构文件的 URI 路径。 SourceUri属性不应在不受信任的方案中公开。 应正确处理异常,以便在不受信任的方案中不公开此敏感信息。

构造函数

名称 说明
XmlSchemaSet()

初始化 XmlSchemaSet 类的新实例。

XmlSchemaSet(XmlNameTable)

用指定的XmlSchemaSet值初始化类的新实例XmlNameTable

属性

名称 说明
CompilationSettings

获取或设置 .XmlSchemaCompilationSettingsXmlSchemaSet

Count

获取 中的 XmlSchemaSet逻辑 XML 架构定义语言 (XSD) 架构的数目。

GlobalAttributes

获取所有 XML 架构定义语言 (XSD) 架构中的所有 XmlSchemaSet全局属性。

GlobalElements

获取所有 XML 架构定义语言 (XSD) 架构中的所有 XmlSchemaSet全局元素。

GlobalTypes

获取所有 XML 架构定义语言 (XSD) 架构中的所有 XmlSchemaSet全局简单和复杂类型。

IsCompiled

获取一个值,该值指示是否已编译 XML XmlSchemaSet 架构定义语言 (XSD) 架构。

NameTable

获取加载新的 XML 架构定义语言 (XSD) 架构时使用的XmlNameTable默认值XmlSchemaSet

XmlResolver

XmlResolver设置用于解析架构的包含和导入元素中引用的命名空间或位置。

方法

名称 说明
Add(String, String)

在指定的 XmlSchemaSetURL 处添加 XML 架构定义语言 (XSD) 架构。

Add(String, XmlReader)

将 XML 架构定义语言 (XSD) 架构中包含的XmlReader架构添加到 .XmlSchemaSet

Add(XmlSchema)

将给定XmlSchema值添加到 .XmlSchemaSet

Add(XmlSchemaSet)

将给定XmlSchemaSet的所有 XML 架构定义语言 (XSD) 架构添加到 。XmlSchemaSet

Compile()

编译添加到 XmlSchemaSet 一个逻辑架构中的 XML 架构定义语言 (XSD) 架构。

Contains(String)

指示具有指定目标命名空间 URI 的 XML 架构定义语言 (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)

XmlSchemaSet中删除指定的 XML 架构定义语言 (XSD) 架构。

RemoveRecursive(XmlSchema)

删除指定的 XML 架构定义语言 (XSD) 架构及其从中 XmlSchemaSet导入的所有架构。

Reprocess(XmlSchema)

重新处理 XML 架构定义语言 (XSD) 架构,该架构已存在于该架构中 XmlSchemaSet

Schemas()

返回所有 XML 架构定义语言 (XSD) 架构的 XmlSchemaSet集合。

Schemas(String)

返回属于给定命名空间的所有 XML 架构定义语言 (XSD) 架构 XmlSchemaSet 的集合。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

活动

名称 说明
ValidationEventHandler

指定一个事件处理程序,用于接收有关 XML 架构定义语言 (XSD) 架构验证错误的信息。

适用于