XmlReaderSettings.Schemas 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置 XmlSchemaSet 在执行架构验证时要使用的值。
public:
property System::Xml::Schema::XmlSchemaSet ^ Schemas { System::Xml::Schema::XmlSchemaSet ^ get(); void set(System::Xml::Schema::XmlSchemaSet ^ value); };
public System.Xml.Schema.XmlSchemaSet Schemas { get; set; }
member this.Schemas : System.Xml.Schema.XmlSchemaSet with get, set
Public Property Schemas As XmlSchemaSet
属性值
在执行 XmlSchemaSet 架构验证时使用。 默认值为空 XmlSchemaSet 对象。
示例
下面的示例使用 XmlReaderSettings 对象和 XmlReader.Create 方法将架构与 XML 文档相关联。 架构将添加到 Schemas 对象的属性 XmlReaderSettings 中。 属性的值 Schemas 是一个 XmlSchemaSet 对象。 该架构用于验证 XML 文档是否符合架构内容模型。 架构验证错误和警告由 ValidationEventHandler 对象中 XmlReaderSettings 定义的处理。
using System;
using System.Xml;
using System.Xml.Schema;
class XmlSchemaSetExample
{
static void Main()
{
XmlReaderSettings booksSettings = new XmlReaderSettings();
booksSettings.Schemas.Add("http://www.contoso.com/books", "books.xsd");
booksSettings.ValidationType = ValidationType.Schema;
booksSettings.ValidationEventHandler += new ValidationEventHandler(booksSettingsValidationEventHandler);
XmlReader books = XmlReader.Create("books.xml", booksSettings);
while (books.Read()) { }
}
static void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e)
{
if (e.Severity == XmlSeverityType.Warning)
{
Console.Write("WARNING: ");
Console.WriteLine(e.Message);
}
else if (e.Severity == XmlSeverityType.Error)
{
Console.Write("ERROR: ");
Console.WriteLine(e.Message);
}
}
}
Imports System.Xml
Imports System.Xml.Schema
Class XmlSchemaSetExample
Shared Sub Main()
Dim booksSettings As XmlReaderSettings = New XmlReaderSettings()
booksSettings.Schemas.Add("http://www.contoso.com/books", "books.xsd")
booksSettings.ValidationType = ValidationType.Schema
AddHandler booksSettings.ValidationEventHandler, New ValidationEventHandler(AddressOf booksSettingsValidationEventHandler)
Dim books As XmlReader = XmlReader.Create("books.xml", booksSettings)
While books.Read()
End While
End Sub
Shared Sub booksSettingsValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
If e.Severity = XmlSeverityType.Warning Then
Console.Write("WARNING: ")
Console.WriteLine(e.Message)
ElseIf e.Severity = XmlSeverityType.Error Then
Console.Write("ERROR: ")
Console.WriteLine(e.Message)
End If
End Sub
End Class
该示例使用 books.xml 文件作为输入。
<bookstore xmlns="http://www.contoso.com/books">
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<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" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
该示例使用 books.xsd 文件作为输入。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bookstore">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string" />
<xs:element minOccurs="0" name="first-name" type="xs:string" />
<xs:element minOccurs="0" name="last-name" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="price" type="xs:decimal" />
</xs:sequence>
<xs:attribute name="genre" type="xs:string" use="required" />
<xs:attribute name="publicationdate" type="xs:unsignedShort" use="required" />
<xs:attribute name="ISBN" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
注解
本文涉及该 Schemas 属性。
Important
- 请勿使用来自未知或不受信任的源或位置的架构。 这样做会损害代码的安全性。
- XML 架构(包括内联架构)本质上容易受到拒绝服务攻击;在不受信任的方案中不接受它们。
- 架构验证错误消息和异常可能会公开有关架构文件的内容模型或 URI 路径的敏感信息。 请注意不要向不受信任的调用方公开此信息。
- 有关详细信息,请参阅“安全注意事项”部分。
该 XmlSchemaSet 类仅支持 XML 架构定义语言 (XSD) 架构。 XmlReader 方法创建的 Create 实例无法配置为启用 XML 数据简化 (XDR) 架构验证。
安全注意事项
不要使用来自未知或不受信任的源的架构。 这样做会损害代码的安全性。 默认情况下,该 XmlUrlResolver 类用于解析外部架构。 若要禁用架构的包含、导入和重新定义元素的解析,请将 XmlSchemaSet.XmlResolver 属性设置为
null。由于使用 XmlSchemaSet 类而引发的异常,例如 XmlSchemaException 类,可能包含不应在不受信任的场景中公开的敏感信息。 例如,属性SourceUriXmlSchemaException返回导致异常的架构文件的 URI 路径。 SourceUri属性不应在不受信任的方案中公开。 应正确处理异常,以便在不受信任的方案中不公开此敏感信息。