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定義的 S 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 方法所創建的實例無法配置為啟用 XML-Data 簡化(XDR)架構驗證。
安全性考慮
請勿使用來自未知或未受信任來源的架構。 這樣做會危害程式代碼的安全性。 類別 XmlUrlResolver 預設用來解析外部架構。 若要停用架構之 include、import 和 redefine 元素的解析,請將 屬性設定 XmlSchemaSet.XmlResolver 為
null。因使用 XmlSchemaSet 類別而引發的例外狀況,例如 類別 XmlSchemaException 可能包含不應在不受信任的案例中公開的敏感性資訊。 例如, SourceUri 的 XmlSchemaException 屬性會傳回造成例外狀況之架構檔案的 URI 路徑。 屬性 SourceUri 不應在不受信任的情境中公開。 應正確處理例外狀況,如此一來,此敏感性資訊就不會在不受信任的案例中公開。