XmlTextReader.XmlResolver 屬性

定義

設定用來解析 DTD 參考的 XmlResolver

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

屬性值

XmlResolver

要使用的 XmlResolver。 如果設定為 null,就不會解析外部資源。

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

範例

下列範例會 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()
{
   
   // Create the reader.
   XmlTextReader^ reader = gcnew XmlTextReader( "http://myServer/data/books.xml" );
   
   // Supply the credentials necessary to access the Web server.
   XmlUrlResolver^ resolver = gcnew XmlUrlResolver;
   resolver->Credentials = CredentialCache::DefaultCredentials;
   reader->XmlResolver = resolver;
   
   // Parse the file.
   while ( reader->Read() )
   {
      
      // Do any additional processing here.
   }

   
   // Close the reader.
   reader->Close();
}
using System;
using System.IO;
using System.Xml;
using System.Net;

public class Sample {

  public static void Main() {

    // Create the reader.
    XmlTextReader reader = new XmlTextReader("http://myServer/data/books.xml");

    // Supply the credentials necessary to access the Web server.
    XmlUrlResolver resolver = new XmlUrlResolver();
    resolver.Credentials = CredentialCache.DefaultCredentials;
    reader.XmlResolver = resolver;

    // Parse the file.
    while (reader.Read()) {
       // Do any additional processing here.
    }

    // Close the reader.
    reader.Close();
  }
}
Imports System.IO
Imports System.Xml
Imports System.Net

public class Sample 

  public shared sub Main() 

    ' Create the reader.
    Dim reader as XmlTextReader = new XmlTextReader("http://myServer/data/books.xml")
   
    ' Supply the credentials necessary to access the Web server.
    Dim resolver as XmlUrlResolver = new XmlUrlResolver()
    resolver.Credentials = CredentialCache.DefaultCredentials
    reader.XmlResolver = resolver

    ' Parse the file.
    while (reader.Read()) 
       ' Do any additional processing here.
    end while           
  
    ' Close the reader.
    reader.Close()     
  
  end sub
end class

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

讀取器會使用 XmlResolver 來解析載入至讀取器之檔案的位置,以及解析 DTD 參考。 例如,如果您的 XML 包含 DOCTYPE 宣告, <!DOCTYPE book SYSTEM book.dtd> 讀取器會解析此外部檔案,並確保 DTD 格式正確。 讀取器不會使用 DTD 進行驗證。

這個屬性可以隨時變更,並在下一個讀取作業上生效。 如果這個屬性設定為 null ,則不會解析讀取器遇到的任何外部 DTD 參考。

在 .NET Framework 1.1 版中,如果未設定此屬性,應用程式的信任層級會決定預設行為。

Fully trusted code: 讀取器會使用沒有使用者認證的預設值 XmlUrlResolver 。 如果需要驗證才能存取網路資源,請使用 XmlResolver 屬性來指定 XmlResolver 具有必要認證的 。

Semi-trusted code: 屬性 XmlResolver 設定為 null 。 不會解析外部資源。

適用於

另請參閱