共用方式為


XmlDocument.XmlResolver 屬性

定義

設定 以 XmlResolver 解決外部資源。

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

屬性值

使用。XmlResolver

在 the.NET Framework 1.1 版本中,呼叫者必須完全信任才能指定 XmlResolver

例外狀況

此屬性設定為 , null 且會遇到外部 DTD 或實體。

範例

以下範例載入一個包含 DTD 檔案參考的 XML 文件。 此 XmlResolver 屬性用來設定存取網路資源所需的憑證。

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

範例使用以下資料檔案作為輸入。

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 或擴充實體參考。 利用這個 XmlResolver.Credentials 屬性,你可以設定 XmlResolver 憑證,存取儲存在安全網路資源上的資源。

  • 如果文件不是用XmlReader串流、檔案XmlResolverXmlDocument等方式載入,則 總是會使用 。

  • 如果文件載入時有 XmlTextReader,則該解析 XmlTextReader 器會用於解析 DocumentType 節點中的任何 DTD 參考。 在 上的 XmlDocument 解析器用於展開任何實體參考。

  • 如果文件載入了 XmlValidatingReader,該解 XmlDocument 算器永遠不會被使用。

  • 如果文件載入了一個擴展XmlReader的類別,且XmlReader無法解析實體(CanResolveEntity回傳 false),XmlResolverXmlDocument則 會被用來解析 DocumentType 節點中的任何參考,並展開任何實體的參考。

備註

如果 是用 有 設定的 XmlResolverXmlReader 載入,之後完成後不會快LoadXmlDocument取 。XmlResolverXmlReaderXmlDocument

在 the.NET Framework 1.1 版本中,若未設定此功能,應用程式的信任等級決定預設行為。

Fully trusted code: 該文件使用預設值 XmlUrlResolver ,沒有使用者憑證。 若需要驗證才能存取網路資源,請使用該 XmlResolver 屬性指定 a XmlResolver 並具備必要的憑證。

Semi-trusted code: XmlResolver屬性設定為 null。 外部資源尚未解決。

欲了解更多關於安全與財產的 XmlResolver 資訊,請參閱 「解決外部資源」。

此屬性是 Microsoft 對文件物件模型(DOM)的擴充。

適用於

另請參閱