XmlResolver XmlResolver XmlResolver XmlResolver Class

定義

解析由統一資源識別元 (URI) 所命名的外部 XML 資源。Resolves external XML resources named by a Uniform Resource Identifier (URI).

public ref class XmlResolver abstract
public abstract class XmlResolver
type XmlResolver = class
Public MustInherit Class XmlResolver
繼承
XmlResolverXmlResolverXmlResolverXmlResolver
衍生

範例

下列範例會建立XmlUrlResolver具有預設認證的。The following example creates an XmlUrlResolver with default credentials. XmlReader是用來讀取和顯示產生的資料流程。A XmlReader is used to read and display the resulting data stream.

using System;
using System.Xml;
using System.IO;

class Example
{
    static void Main()
    {
        // Create an XmlUrlResolver with default credentials.
        XmlUrlResolver resolver = new XmlUrlResolver();
        resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;

        // Point the resolver at the desired resource and resolve as a stream.
        Uri baseUri = new Uri("http://serverName/");
        Uri fulluri = resolver.ResolveUri(baseUri, "fileName.xml");
        Stream s = (Stream)resolver.GetEntity(fulluri, null, typeof(Stream));

        // Create the reader with the resolved stream and display the data.
        XmlReader reader = XmlReader.Create(s);
        while (reader.Read())
        {
            Console.WriteLine(reader.ReadOuterXml());
        }
    }
}
Imports System.Xml
Imports System.IO

Module Module1

    Sub Main()
        ' Create an XmlUrlResolver with default credentials.
        Dim resolver As New XmlUrlResolver()
        resolver.Credentials = System.Net.CredentialCache.DefaultCredentials

        ' Point the resolver at the desired resource and resolve as a stream.
        Dim baseUri As New Uri("http://serverName/")
        Dim fulluri As Uri = resolver.ResolveUri(baseUri, "fileName.xml")
        Dim s As Stream = CType(resolver.GetEntity(fulluri, Nothing, GetType(Stream)), Stream)

        ' Create the reader with the resolved stream and display the data.
        Dim reader As XmlReader = XmlReader.Create(s)
        While reader.Read()
            Console.WriteLine(reader.ReadOuterXml())
        End While
    End Sub
End Module

備註

XmlResolver類型是用來解析外部 XML 資源,例如實體、檔案類型定義(dtd)或架構。The XmlResolver type is used to resolve external XML resources, such as entities, document type definitions (DTDs), or schemas. 它也可用來處理可延伸樣式單語言(XSL)樣式表單或 XML 架構定義語言(XSD)架構中找到的 include 和 import 元素。It is also used to process include and import elements found in Extensible Stylesheet Language (XSL) style sheets or XML Schema definition language (XSD) schemas.

XmlResolver處理協調資源連接的所有層面,包括處理安全性認證、開啟資料來源的連接,以及以資料流程或其他物件類型的形式傳回資源。XmlResolver handles all aspects of negotiating the connection to the resources, including handling security credentials, opening the connection to the data source, and returning the resource in the form of a stream or other object type. 呼叫XmlResolver的物件具有解讀資料流程的工作。The object that calls XmlResolver has the task of interpreting the stream.

命名空間包含XmlResolver類別的兩個具體實作為: System.XmlThe System.Xml namespace includes two concrete implementations of the XmlResolver class:

您可以建立並指定自己的解析程式。You can create and specify your own resolver. 如果您未指定解析程式,則讀取器會使用XmlUrlResolver預設值,而不使用使用者認證。If you don't specify a resolver, the reader uses a default XmlUrlResolver with no user credentials.

藉由設定 XmlResolver 屬性並將 XmlReaderSettings.XmlResolver 物件傳遞至 XmlReaderSettings 方法,可以指定要使用的 CreateYou specify the XmlResolver to use by setting the XmlReaderSettings.XmlResolver property and passing the XmlReaderSettings object to the Create method.

如果資源儲存在需要驗證的系統上,您可以使用XmlResolver.Credentials屬性來指定所需的認證。If the resource is stored on a system that requires authentication, you use the XmlResolver.Credentials property to specify the necessary credentials.

提供驗證認證Supplying authentication credentials

包含要讀取之 XML 資料的檔案可能會有限制存取原則。The file that contains the XML data to read may have a restricted access policy. 如果需要驗證才可存取網路資源,請使用 Credentials 屬性指定必要的認證。If authentication is required to access a network resource, use the Credentials property to specify the necessary credentials. 如果未設定null屬性,則會將Credentials認證設定為。If the Credentials property is not set, credentials are set to null.

例如,假設從 web 要求資料以進行驗證時,需要認證。For example, assume that credentials are needed when requesting data from the web for authentication purposes. 除非 web 虛擬目錄允許匿名存取,否則您必須設定Credentials屬性來提供認證。Unless the web virtual directory allows anonymous access, you must set the Credentials property to supply credentials. 下列範例會建立一個XmlReader物件,它會XmlUrlResolver使用具有http://localhost/bookstore/inventory.xml預設認證的來存取網站。The following example creates an XmlReader object that uses an XmlUrlResolver with default credentials to access the http://localhost/bookstore/inventory.xml site.

// Create a resolver with default credentials.
XmlUrlResolver^ resolver = gcnew XmlUrlResolver;
resolver->Credentials = System::Net::CredentialCache::DefaultCredentials;

 // Set the reader settings object to use the resolver.
 settings->XmlResolver = resolver;

// Create the XmlReader object.
XmlReader^ reader = XmlReader::Create( L"http://ServerName/data/books.xml", settings );

  // Create a resolver with default credentials.
  XmlUrlResolver resolver = new XmlUrlResolver();
  resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;

  // Set the reader settings object to use the resolver.
  settings.XmlResolver = resolver;

  // Create the XmlReader object.
  XmlReader reader = XmlReader.Create("http://ServerName/data/books.xml", settings);
' Create a resolver with default credentials.
Dim resolver as XmlUrlResolver = new XmlUrlResolver()
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials

' Set the reader settings object to use the resolver.
settings.XmlResolver = resolver

' Create the XmlReader object.
Dim reader as XmlReader = XmlReader.Create("http://ServerName/data/books.xml", settings)

您可以為不同的 Uri 提供不同的認證,並將它們新增至快取。You can supply different credentials for different URIs and add them to a cache. 不論 XML 的原始來源為何,這些認證都會用於檢查不同 URI 的驗證。These credentials are used to check authentication for the different URIs regardless of the original source of the XML. 下列範例顯示如何將認證新增至快取。The following example shows how to add credentials to a cache.

// Create the credentials.
NetworkCredential myCred = new NetworkCredential(UserName,SecurelyStoredPassword,Domain); 
CredentialCache myCache = new CredentialCache(); 
myCache.Add(new Uri("http://www.contoso.com/"), "Basic", myCred); 
myCache.Add(new Uri("http://app.contoso.com/"), "Basic", myCred);

// Set the credentials on the XmlUrlResolver object.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = myCache;

// Compile the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("http://serverName/data/xsl/order.xsl",XsltSettings.Default, resolver);	
' Create the credentials.
Dim myCred As NetworkCredential = New NetworkCredential(UserName,SecurelyStoredPassword,Domain)
Dim myCache As CredentialCache = New CredentialCache()
myCache.Add(new Uri("http://www.contoso.com/"), "Basic", myCred)
myCache.Add(new Uri("http://app.contoso.com/"), "Basic", myCred)

' Set the credentials on the XmlUrlResolver object.
Dim resolver As XmlUrlResolver = New XmlUrlResolver()
resolver.Credentials = myCache

' Compile the style sheet.
Dim xslt As XslCompiledTransform = New XslCompiledTransform()
xslt.Load("http://serverName/data/xsl/order.xsl", XsltSettings.Default, resolver)

安全性考量Security considerations

使用XmlResolver類別時,請考慮下列專案。Consider the following items when working with the XmlResolver class.

  • XmlResolver 物件可以包含機密資訊 (如使用者認證)。XmlResolver objects can contain sensitive information such as user credentials. 在快取XmlResolver物件時應該小心,而且不應該將XmlResolver物件傳遞至不受信任的元件。You should be careful when caching XmlResolver objects and should not pass the XmlResolver object to an untrusted component.

  • 如果設計使用 XmlResolver 類別的類別屬性,應將屬性定義為唯寫屬性。If you are designing a class property that uses the XmlResolver class, the property should be defined as a write-only property. 該屬性可用於指定要使用的 XmlResolver,但其不可用於傳回 XmlResolver 物件。The property can be used to specify the XmlResolver to use, but it cannot be used to return an XmlResolver object.

  • 如果您的應用XmlResolver程式接受來自不受信任程式碼的物件,您就無法GetEntity假設傳遞至方法的 URI 會與ResolveUri方法所傳回的 URI 相同。If your application accepts XmlResolver objects from untrusted code, you cannot assume that the URI passed into the GetEntity method will be the same as that returned by the ResolveUri method. XmlResolver 類別衍生的類別可以覆寫 GetEntity 方法,並傳回與原始 URI 所包含之內容不同的資料。Classes derived from the XmlResolver class can override the GetEntity method and return data that is different than what was contained in the original URI.

  • 您的應用程式可以藉由GetEntity IStream執行限制讀取的位元組數目,來減輕對方法的記憶體阻絕服務威脅。Your application can mitigate memory denial of service threats to the GetEntity method by implementing an IStream that limits the number of bytes read. 這有助於防範惡意程式碼嘗試將無限位元組資料流程傳遞至方法的GetEntity情況。This helps guard against situations where malicious code attempts to pass an infinite stream of bytes to the GetEntity method.

建構函式

XmlResolver() XmlResolver() XmlResolver() XmlResolver()

初始化 XmlResolver 類別的新執行個體。Initializes a new instance of the XmlResolver class.

屬性

Credentials Credentials Credentials Credentials

在衍生類別中覆寫時,設定用來驗證 Web 要求的認證資訊。When overridden in a derived class, sets the credentials used to authenticate web requests.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEntity(Uri, String, Type) GetEntity(Uri, String, Type) GetEntity(Uri, String, Type) GetEntity(Uri, String, Type)

在衍生類別中覆寫時,對應 URI 至包含實際資源的物件。When overridden in a derived class, maps a URI to an object that contains the actual resource.

GetEntityAsync(Uri, String, Type) GetEntityAsync(Uri, String, Type) GetEntityAsync(Uri, String, Type) GetEntityAsync(Uri, String, Type)

以非同步的方式將 URI 對應至包含實際資源的物件。Asynchronously maps a URI to an object that contains the actual resource.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ResolveUri(Uri, String) ResolveUri(Uri, String) ResolveUri(Uri, String) ResolveUri(Uri, String)

在衍生類別中覆寫時,從基底 (Base) 和相對 URI 來解析絕對 URI。When overridden in a derived class, resolves the absolute URI from the base and relative URIs.

SupportsType(Uri, Type) SupportsType(Uri, Type) SupportsType(Uri, Type) SupportsType(Uri, Type)

讓解析程式傳回 Stream 以外的類型。Enables the resolver to return types other than Stream.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於

另請參閱