XmlReaderSettings.ValidationType 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个值,该值指示 XmlReader 在读取时是否执行验证或类型分配。
public:
property System::Xml::ValidationType ValidationType { System::Xml::ValidationType get(); void set(System::Xml::ValidationType value); };
public System.Xml.ValidationType ValidationType { get; set; }
member this.ValidationType : System.Xml.ValidationType with get, set
Public Property ValidationType As ValidationType
属性值
ValidationType 的一个值指示 XmlReader 是否将呈现有效或当读取的时执行验证或类型分配。 默认值为 ValidationType.None
。
示例
以下示例使用存储在 .. 中的 XmlSchemaSet架构进行验证。
#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'.
此示例使用以下输入文件:
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>
注解
下表描述了 ValidationType 这些值。
ValidationType | 说明 |
---|---|
DTD |
使用 DTD) (文档类型定义执行验证。 注意: 该 DtdProcessing 属性还必须设置为 Parse。 |
None |
不 XmlReader 验证数据或执行任何类型分配。 |
Schema |
使用 XML 架构定义语言 (XSD) 架构执行验证和类型分配。 读取器使用以下项访问 XML 架构: - 使用该 Schemas 属性访问 XmlSchemaSet 与此读取器关联的对象。 - 使用 XML 实例文档中包含的内联架构。 ProcessInlineSchema (必须启用此选项。) - 使用 XML 实例文档中找到的架构位置提示 ( xsi:schemaLocation 或 xsi:noNamespaceSchemaLocation 属性) 指定的 XML 架构。 ProcessSchemaLocation (必须启用此选项。) |