XmlValidatingReader.XmlResolver 属性
定义
设置用于解析外部文档类型定义 (DTD) 和架构位置引用的 XmlResolver。Sets the XmlResolver used for resolving external document type definition (DTD) and schema location references. XmlResolver 用于处理 XML 架构定义语言 (XSD) 架构中找到的任何 import 或 include 元素。The XmlResolver is also used to handle any import or include elements found in XML Schema definition language (XSD) schemas.
public:
property System::Xml::XmlResolver ^ XmlResolver { void set(System::Xml::XmlResolver ^ value); };
public System.Xml.XmlResolver XmlResolver { set; }
member this.XmlResolver : System.Xml.XmlResolver
Public Property XmlResolver As XmlResolver
属性值
要使用的 XmlResolver。The XmlResolver to use. 如果设置为 null,则不解析外部资源。If set to null, external resources are not resolved.
在 .NET Framework 版本1.1 中,调用方必须完全受信任才能指定 XmlResolver 。In version 1.1 of the .NET Framework, the caller must be fully trusted to specify an XmlResolver.
示例
下面的示例使用 XmlResolver 属性指定访问网络 DTD 文件所需的凭据。The following example uses the XmlResolver property to specify the credentials necessary to access the networked DTD file.
// Create the reader. ->
XmlTextReader^ txtreader = gcnew XmlTextReader( "book5.xml" );
XmlValidatingReader^ reader = gcnew XmlValidatingReader( txtreader );
txtreader->WhitespaceHandling = WhitespaceHandling::None;
// Set the credentials necessary to access the DTD file stored on the network.
XmlUrlResolver^ resolver = gcnew XmlUrlResolver;
resolver->Credentials = System::Net::CredentialCache::DefaultCredentials;
reader->XmlResolver = resolver;
// Display each of the element nodes.
while ( reader->Read() )
{
switch ( reader->NodeType )
{
case XmlNodeType::Element:
Console::Write( "< {0}>", reader->Name );
break;
case XmlNodeType::Text:
Console::Write( reader->Value );
break;
case XmlNodeType::DocumentType:
Console::Write( "<!DOCTYPE {0} [ {1}]", reader->Name, reader->Value );
break;
case XmlNodeType::EntityReference:
Console::Write( reader->Name );
break;
case XmlNodeType::EndElement:
Console::Write( "</ {0}>", reader->Name );
break;
}
}
// Close the reader.
reader->Close();
}
// Create the reader.
XmlTextReader txtreader = new XmlTextReader("book5.xml");
XmlValidatingReader reader = new XmlValidatingReader(txtreader);
txtreader.WhitespaceHandling = WhitespaceHandling.None;
// Set the credentials necessary to access the DTD file stored on the network.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
reader.XmlResolver = resolver;
// Display each of the element nodes.
while (reader.Read()){
switch (reader.NodeType){
case XmlNodeType.Element:
Console.Write("<{0}>", reader.Name);
break;
case XmlNodeType.Text:
Console.Write(reader.Value);
break;
case XmlNodeType.DocumentType:
Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value);
break;
case XmlNodeType.EntityReference:
Console.Write(reader.Name);
break;
case XmlNodeType.EndElement:
Console.Write("</{0}>", reader.Name);
break;
}
}
' Create the reader.
Dim txtreader as XmlTextReader = new XmlTextReader("book5.xml")
Dim reader as XmlValidatingReader = new XmlValidatingReader(txtreader)
txtreader.WhitespaceHandling = WhitespaceHandling.None
' Set the credentials necessary to access the DTD file stored on the network.
Dim resolver as XmlUrlResolver = new XmlUrlResolver()
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials
reader.XmlResolver = resolver
' Display each of the element nodes.
while (reader.Read())
select case reader.NodeType
case XmlNodeType.Element:
Console.Write("<{0}>", reader.Name)
case XmlNodeType.Text:
Console.Write(reader.Value)
case XmlNodeType.DocumentType:
Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value)
case XmlNodeType.EntityReference:
Console.Write(reader.Name)
case XmlNodeType.EndElement:
Console.Write("</{0}>", reader.Name)
end select
end while
该示例使用以下文件作为输入。The example uses the following files as input.
book5.xml
<!DOCTYPE book SYSTEM 'http://myServer/DTDs/books.dtd'>
<book ISBN = '1-861001-57-5'>
<title>Pride And Prejudice</title>
<price>19.95</price>
<misc>&h;</misc>
</book>
books.dtd
<!ELEMENT book (title,price,misc)>
<!ATTLIST book
genre CDATA "novel"
ISBN CDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT misc (#PCDATA)>
<!ENTITY h "hardcover">
<!ENTITY p "paperback">
注解
备注
XmlValidatingReader类在 .NET Framework 2.0 中已过时。The XmlValidatingReader class is obsolete in .NET Framework 2.0. 您可以 XmlReader 使用 XmlReaderSettings 类和方法创建验证实例 Create 。You can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. 有关详细信息,请参阅 XmlReader 引用页的“备注”部分。For more information, see the Remarks section of the XmlReader reference page.
XmlResolver用于加载完成验证过程所需的任何 dtd、实体或架构。The XmlResolver is used to load any DTDs, entities, or schemas needed to complete the validation process.
此属性可随时设置,并会在下一次调用之后生效 Read 。This property can be set at any time and takes affect after the next Read call. 如果将此属性设置为 null ,则读取器将假定用户不需要解析外部引用。If this property is set to null, the reader assumes the user is not interested in resolving external references. 在这种情况下,仅当资源存在时,读取器才会验证内部资源。In this case, the reader only validates against internal resources, if the resource is present.
在 .NET Framework 版本1.1 中,如果未设置此属性,则应用程序的信任级别将确定默认行为。In version 1.1 of .NET Framework , if this property is not set, the trust level of the application determines the default behavior.
Fully trusted code: 读取器使用 XmlUrlResolver 没有用户凭据的默认值。Fully trusted code: The reader uses a default XmlUrlResolver with no user credentials. 如果需要身份验证才能访问网络资源,请使用 XmlResolver 属性指定 XmlResolver 具有必要凭据的。If authentication is required to access a network resource, use the XmlResolver property to specify an XmlResolver with the necessary credentials.
Semi-trusted code:``XmlResolver属性设置为 null 。Semi-trusted code: The XmlResolver property is set to null. 不解析外部资源。External resources are not resolved.
使用架构进行验证时,可以通过使用属性提供来避免昂贵的加载过程 XmlSchemaCollection Schemas 。When validating using schemas, you can avoid the expensive load process by providing an XmlSchemaCollection using the Schemas property.