XmlReaderSettings.DtdProcessing 属性
定义
获取或设置确定 DTD 的处理的值。Gets or sets a value that determines the processing of DTDs.
public:
property System::Xml::DtdProcessing DtdProcessing { System::Xml::DtdProcessing get(); void set(System::Xml::DtdProcessing value); };
public System.Xml.DtdProcessing DtdProcessing { get; set; }
member this.DtdProcessing : System.Xml.DtdProcessing with get, set
Public Property DtdProcessing As DtdProcessing
属性值
确定 DTD 的处理的枚举值之一。One of the enumeration values that determines the processing of DTDs. 默认值为 Prohibit。The default is Prohibit.
示例
以下示例使用 DTD 文件验证 XML 文件。The following example validates an XML file using a DTD file.
#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: {0}", e->Message );
}
int main()
{
// Set the validation settings.
XmlReaderSettings^ settings = gcnew XmlReaderSettings;
settings->DtdProcessing = DtdProcessing::Parse;
settings->ValidationType = ValidationType::DTD;
settings->ValidationEventHandler += gcnew ValidationEventHandler( ValidationCallBack );
// Create the XmlReader object.
XmlReader^ reader = XmlReader::Create( L"itemDTD.xml", settings );
// Parse the file.
while ( reader->Read() )
;
return 1;
}
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
public class Sample {
public static void Main() {
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
settings.ValidationType = ValidationType.DTD;
settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
// Create the XmlReader object.
XmlReader reader = XmlReader.Create("itemDTD.xml", settings);
// Parse the file.
while (reader.Read());
}
// Display any validation errors.
private static void ValidationCallBack(object sender, ValidationEventArgs e) {
Console.WriteLine("Validation Error: {0}", e.Message);
}
}
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
public class Sample
public shared sub Main()
' Set the validation settings.
Dim settings as XmlReaderSettings = new XmlReaderSettings()
settings.DtdProcessing = DtdProcessing.Parse
settings.ValidationType = ValidationType.DTD
AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack
' Create the XmlReader object.
Dim reader as XmlReader = XmlReader.Create("itemDTD.xml", settings)
' Parse the file.
while reader.Read()
end while
end sub
' Display any validation errors.
private shared sub ValidationCallBack(sender as object, e as ValidationEventArgs)
Console.WriteLine("Validation Error: {0}", e.Message)
end sub
end class
输入Input
示例使用 itemDTD.xml
文件作为输入。The example uses the itemDTD.xml
file as input.
<!--XML file using a DTD-->
<!DOCTYPE store [
<!ELEMENT store (item)*>
<!ELEMENT item (name,dept,price)>
<!ATTLIST item type CDATA #REQUIRED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT price (#PCDATA)>]>
<store>
<item type="supplies" ISBN="2-3631-4">
<name>paint</name>
<price>16.95</price>
</item>
</store>
注解
(DTD) 验证的文档类型定义是使用 W3C 可扩展标记语言 (XML) 1.0 (第四版中定义的有效性约束来实现的。Document type definition (DTD) validation is implemented by using the validity constraints defined in the W3C Extensible Markup Language (XML) 1.0 (fourth edition) recommendation. Dtd 使用正式语法来描述符合标准的 XML 文档的结构和语法;它们指定 XML 文档所允许的内容和值。DTDs use a formal grammar to describe the structure and syntax of compliant XML documents; they specify the content and values allowed for the XML document.
此属性可以具有下列值之一:This property can have one of the following values:
DtdProcessing.Parse 启用 DTD 处理。DtdProcessing.Parse to enable DTD processing.
DtdProcessing.ProhibitXmlException当遇到 DTD 时引发异常。DtdProcessing.Prohibit to throw an XmlException exception when a DTD is encountered.
DtdProcessing.Ignore 禁用 DTD 处理但不发出警告或异常。DtdProcessing.Ignore to disable DTD processing without warnings or exceptions.
为针对 DTD 执行验证,XmlReader 使用 XML 文档的 DOCTYPE 声明中所定义的 DTD。To perform validation against a DTD, the XmlReader uses the DTD defined in the DOCTYPE declaration of an XML document. DOCTYPE 声明既可以指向内联 DTD,也可以是对外部 DTD 文件的引用。The DOCTYPE declaration can either point to an inline DTD or can be a reference to an external DTD file. 针对 DTD 验证 XML 文件:To validate an XML file against a DTD:
将 XmlReaderSettings.DtdProcessing 属性设置为
DtdProcessing.Parse.
Set the XmlReaderSettings.DtdProcessing property toDtdProcessing.Parse.
将 XmlReaderSettings.ValidationType 属性设置为
ValidationType.DTD
。Set the XmlReaderSettings.ValidationType property toValidationType.DTD
.如果 DTD 是存储在要求进行身份验证的网络资源上的外部文件,请将具有必要凭据的 XmlResolver 对象传递给 Create 方法。If the DTD is an external file stored on a network resource that requires authentication, pass an XmlResolver object with the necessary credentials to the Create method.
重要
如果将 DtdProcessing 属性设置为 DtdProcessing.Ignore ,则 XmlReader 不会报告 dtd。If the DtdProcessing property is set to DtdProcessing.Ignore, the XmlReader will not report the DTDs. 这意味着,DTD/DOCTYPE 将在输出时丢失。This means that the DTD/DOCTYPE will be lost on output.