XmlDocument.XmlResolver 屬性

定義

設定用於解析外部資源的 XmlResolverSets 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.

範例

下列範例會載入包含 DTD 檔案參考的 XML 檔。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在上設定認證, 以存取儲存在安全網路資源上的資源。 XmlResolver.CredentialsUsing the XmlResolver.Credentials property, you can set credentials on the XmlResolver to access resources stored on a secure network resource.

  • 如果檔未使用XmlReader載入 (也就是使用資料流程、檔案等載入XmlResolver ), 一律XmlDocument會使用上的。If 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 , 而且false無法解析實體 (CanResolveEntity傳回), 則XmlDocument會使用XmlResolver上的來解析 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.

注意

XmlResolver XmlDocument Load XmlReader如果是使用XmlReader 已設定的來載入,則在完成之後就不會快取上的。XmlResolver XmlDocumentIf 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.

在1.1 版的 the.NET Framework 中, 如果未設定此屬性, 應用程式的信任層級會決定預設行為。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:檔會使用預設值XmlUrlResolver , 而不使用使用者認證。Fully trusted code: The document 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:屬性會設定為nullXmlResolverSemi-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.

這個屬性是文件物件模型的 Microsoft 擴充功能。This property is a Microsoft extension to the Document Object Model (DOM).

適用於

另請參閱