XmlDocument.XmlResolver 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
設定用於解析外部資源的 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。
在 .NET Framework 1.1 中,必須完全信任呼叫端,才能指定 XmlResolver。
例外狀況
這個屬性設定為 null 且遇到外部 DTD 或實體。
範例
下列範例會載入包含 DTD 檔案參考的 XML 檔。 XmlResolver屬性可用來設定存取網路資源所需的認證。
#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
此範例會使用下列資料檔案作為輸入。
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 (載入,則如果檔是使用資料流程、檔案等
XmlResolver載入,則一律會使用 上的XmlDocument) 。如果檔已載入 XmlTextReader ,則 上的
XmlTextReader解析程式會用來解析 DocumentType 節點中的任何 DTD 參考。 上的XmlDocument解析程式可用來展開任何實體參考。如果檔已載入 XmlValidatingReader ,則永遠不會使用 上的
XmlDocument解析程式。如果檔是以擴充
XmlReader的類別載入,且 無法XmlReader解析實體 (CanResolveEntity 傳回false) ,則XmlResolver上的XmlDocument會用來解析 DocumentType 節點中的任何參考,並展開任何實體參考。
注意
XmlDocument如果使用已設定的 XmlResolver 載入 XmlReader ,則 XmlResolver 完成之後 Load ,就不會快 XmlDocument 取 上的 XmlReader 。
在 the.NET Framework 1.1 版中,如果未設定此屬性,應用程式的信任層級會決定預設行為。
Fully trusted code: 檔使用預設值 XmlUrlResolver ,不含使用者認證。 如果需要驗證才能存取網路資源,請使用 XmlResolver 屬性來指定 XmlResolver 具有必要認證的 。
Semi-trusted code: 屬性 XmlResolver 設定為 null 。 不會解析外部資源。
如需安全性和 XmlResolver 屬性的詳細資訊,請參閱 解析外部資源。
這個屬性是文件物件模型的 Microsoft 擴充功能。