XmlDocument.XmlResolver 属性

设置 XmlResolver 以用于解析外部资源。

**命名空间:**System.Xml
**程序集:**System.Xml(在 system.xml.dll 中)

语法

声明
Public Overridable WriteOnly Property XmlResolver As XmlResolver
用法
Dim instance As XmlDocument
Dim value As XmlResolver

instance.XmlResolver = value
public virtual XmlResolver XmlResolver { set; }
public:
virtual property XmlResolver^ XmlResolver {
    void set (XmlResolver^ value);
}
/** @property */
public void set_XmlResolver (XmlResolver value)
public function set XmlResolver (value : XmlResolver)

属性值

要使用的 XmlResolver。 在 .NET Framework 1.1 版中,调用方必须受到完全信任才能指定 XmlResolver

异常

异常类型 条件

XmlException

该属性被设置为 空引用(在 Visual Basic 中为 Nothing) 并且遇到了外部 DTD 或实体。

备注

XmlResolver 可用于加载 DTD 或展开实体引用。使用 XmlResolver.Credentials 属性,您可以在 XmlResolver 上设置凭据以访问存储在安全网络资源上的资源。

  • 如果文档不是使用 XmlReader 加载的(也就是说,如果文档是使用流、文件等加载的),则总是使用 XmlDocument 上的 XmlResolver

  • 如果文档是通过 XmlTextReader 加载的,则 XmlTextReader 上的解析器用于解析 DocumentType 节点中的任何 DTD 引用。XmlDocument 上的解析器用于展开任何实体引用。

  • 如果文档是通过 XmlValidatingReader 加载的,则从不使用 XmlDocument 上的解析器。

  • 如果文档是通过扩展 XmlReader 的类加载的,而该 XmlReader 不能解析实体(CanResolveEntity 返回 false),则 XmlDocument 上的 XmlResolver 用于解析 DocumentType 节点中的任何引用和展开任何实体引用。

提示

如果 XmlDocument 是使用 XmlReader(具有一个为它设置的 XmlResolver)加载的,则在 Load 完成之后 XmlDocument 不缓存 XmlReader 上的 XmlResolver

在 .NET Framework 1.1 版中,如果未设置该属性,则由应用程序的信任级别确定默认行为。

Fully trusted code: 文档使用没有用户凭据的默认 XmlUrlResolver。如果需要身份验证才能访问网络资源,请使用 XmlResolver 属性指定具有必需凭据的 XmlResolver

Semi-trusted code:XmlResolver 属性被设置为 空引用(在 Visual Basic 中为 Nothing)。不解析外部资源。

有关安全和 XmlResolver 属性的更多信息,请参见 解析外部资源

该属性是文档对象模型 (DOM) 的 Microsoft 扩展。

示例

下面的示例加载包括对 DTD 文件的引用的 XML 文档。XmlResolver 属性用于设置访问网络资源所必需的凭据。

Imports System
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
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
#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 );
}

该示例将下列数据文件用作输入。

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">

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

XmlDocument 类
XmlDocument 成员
System.Xml 命名空间
Credentials
System.Net.CredentialCache
System.Net.NetworkCredential
XmlSecureResolver