XmlDocument.XmlResolver 属性

定义

设置 XmlResolver 以用于解析外部资源。Sets the XmlResolver to use for resolving external resources.

public:
 virtual property System::Xml::XmlResolver ^ XmlResolver {  void set(System::Xml::XmlResolver ^ value); };
public virtual System.Xml.XmlResolver XmlResolver { set; }
member this.XmlResolver : System.Xml.XmlResolver
Public Overridable Property XmlResolver As XmlResolver

属性值

要使用的 XmlResolverThe XmlResolver to use.

在 .NET Framework 1.1 版中,调用方必须受到完全信任才能指定 XmlResolverIn version 1.1 of the.NET Framework, the caller must be fully trusted in order to specify an XmlResolver.

例外

此属性设置为 null,并且遇到了外部 DTD 或实体。This property is set to null and an external DTD or entity is encountered.

示例

下面的示例加载一个 XML 文档,该文档包含对 DTD 文件的引用。The following example loads an XML document which includes a reference to a DTD file. XmlResolver 属性用于设置访问网络资源所需的凭据。The XmlResolver property is used to set the credentials necessary to access the network resource.

#using <System.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Net;
int main()
{
   
   // Supply the credentials necessary to access the DTD file stored on the network.
   XmlUrlResolver^ resolver = gcnew XmlUrlResolver;
   resolver->Credentials = CredentialCache::DefaultCredentials;
   
   // Create and load the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   doc->XmlResolver = resolver; // Set the resolver.
   doc->Load( "book5.xml" );
   
   // Display the entity replacement text which is pulled from the DTD file.
   Console::WriteLine( doc->DocumentElement->LastChild->InnerText );
}

using System;
using System.IO;
using System.Xml;
using System.Net;

public class Sample {

  public static void Main() {

    // Supply the credentials necessary to access the DTD file stored on the network.
    XmlUrlResolver resolver = new XmlUrlResolver();
    resolver.Credentials = CredentialCache.DefaultCredentials;

    // Create and load the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.XmlResolver = resolver;  // Set the resolver.
    doc.Load("book5.xml");

    // Display the entity replacement text which is pulled from the DTD file.
    Console.WriteLine(doc.DocumentElement.LastChild.InnerText);
  }
} // End class
Imports System.IO
Imports System.Xml
Imports System.Net

public class Sample 

  public shared sub Main()

    ' Supply the credentials necessary access the DTD file stored on the network.
    Dim resolver as XmlUrlResolver = new XmlUrlResolver()
    resolver.Credentials = CredentialCache.DefaultCredentials

    ' Create and load the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    doc.XmlResolver = resolver  ' Set the resolver.
    doc.Load("book5.xml")

    ' Display the entity replacement text which is pulled from the DTD file.
    Console.WriteLine(doc.DocumentElement.LastChild.InnerText)
  
  end sub
end class

该示例使用以下数据文件作为输入。The example uses the following data files as input.

book5.xml

<!DOCTYPE book SYSTEM 'http://myServer/data/books.dtd'>
<book ISBN = '1-861001-57-5'>
  <title>Oberon's Legacy</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">

注解

XmlResolver 可用于加载 Dtd 或扩展实体引用。The XmlResolver can be used to load DTDs or expand entity references. 使用 XmlResolver.Credentials 属性,你可以在 XmlResolver 上设置凭据,以访问存储在安全网络资源上的资源。Using the XmlResolver.Credentials property, you can set credentials on the XmlResolver to access resources stored on a secure network resource.

  • 如果未使用 XmlReader 加载文档(即,如果使用流、文件等加载,则始终使用 XmlDocument 上的 XmlResolverIf the document was not loaded using an XmlReader (that is, if it was loaded using a stream, file, and so on) the XmlResolver on the XmlDocument is always used.

  • 如果文档是使用 XmlTextReader加载的,则 XmlTextReader 上的解析程序用于解析 DocumentType 节点中的任何 DTD 引用。If the document was loaded with an XmlTextReader, the resolver on the XmlTextReader is used to resolve any DTD references in the DocumentType node. XmlDocument 上的解析程序用于展开任何实体引用。The resolver on the XmlDocument is used to expand any entity references.

  • 如果文档是使用 XmlValidatingReader加载的,则从不使用 XmlDocument 上的解析程序。If the document was loaded with an XmlValidatingReader, the resolver on the XmlDocument is never used.

  • 如果文档是使用扩展 XmlReader 的类加载的并且 XmlReader 无法解析实体(CanResolveEntity 返回 false),则 XmlResolver 上的 XmlDocument 用于解析 DocumentType 节点中的任何引用并展开任何实体引用。If the document was loaded with a class that extends XmlReader and the XmlReader cannot resolve entities (CanResolveEntity returns false), the XmlResolver on the XmlDocument is used to resolve any references in the DocumentType node and to expand any entity references.

备注

如果 XmlDocument 是使用设置了 XmlResolverXmlReader 加载的,则 XmlReader 完成后,XmlDocument 不会缓存 Load 上的 XmlResolverIf the XmlDocument is loaded using an XmlReader which had an XmlResolver set to it, the XmlResolver on the XmlReader is not cached by the XmlDocument after Load completes.

在 the.NET Framework 版本1.1 中,如果未设置此属性,则应用程序的信任级别将确定默认行为。In version 1.1 of the.NET Framework, if this property is not set, the trust level of the application determines the default behavior.

Fully trusted code: 文档使用没有用户凭据的默认 XmlUrlResolverFully trusted code: The document uses a default XmlUrlResolver with no user credentials. 如果访问网络资源需要身份验证,请使用 XmlResolver 属性指定具有所需凭据的 XmlResolverIf authentication is required to access a network resource, use the XmlResolver property to specify an XmlResolver with the necessary credentials.

Semi-trusted code: XmlResolver 属性设置为 nullSemi-trusted code: The XmlResolver property is set to null. 不解析外部资源。External resources are not resolved.

有关安全性和 XmlResolver 属性的详细信息,请参阅解析外部资源For more information on security and the XmlResolver property, see Resolving External Resources.

此属性是文档对象模型 (DOM) 的 Microsoft 扩展。This property is a Microsoft extension to the Document Object Model (DOM).

适用于

另请参阅