XmlUrlResolver 類別

定義

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

public ref class XmlUrlResolver : System::Xml::XmlResolver
public class XmlUrlResolver : System.Xml.XmlResolver
type XmlUrlResolver = class
    inherit XmlResolver
Public Class XmlUrlResolver
Inherits XmlResolver
繼承
XmlUrlResolver

範例

下列範例會建立使用預設認證之 XmlUrlResolverXmlReaderThe following example creates an XmlReader that uses an XmlUrlResolver with default credentials.


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

 // Create the reader.
 XmlReaderSettings settings = new XmlReaderSettings();
 settings.XmlResolver = resolver;
 XmlReader reader = 
      XmlReader.Create("http://serverName/data/books.xml", settings);

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

' Create the reader.
Dim settings As New XmlReaderSettings()
settings.XmlResolver = resolver
Dim reader As XmlReader = _
   XmlReader.Create("http://serverName/data/books.xml", settings)

備註

XmlUrlResolver 可用來解析外部 XML 資源,例如實體、檔案類型定義(Dtd)或架構。XmlUrlResolver 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.

XmlUrlResolverSystem.Xml 命名空間中所有類別的預設解析程式。XmlUrlResolver is the default resolver for all classes in the System.Xml namespace. 它支援 file://http:// 通訊協定,以及 WebRequest 類別的要求。It supports the file:// and http:// protocols and requests from the WebRequest class.

重要

XmlUrlResolver 物件可以包含機密資訊 (如使用者認證)。XmlUrlResolver objects can contain sensitive information such as user credentials. 當您快取 XmlUrlResolver 物件,且不應將 XmlUrlResolver 物件傳遞至不受信任的元件時,請務必小心。You should be careful when you cache XmlUrlResolver objects and should not pass XmlUrlResolver objects to an untrusted component.

解析 DtdResolving DTDs

如果 XML 讀取器(XmlReader)正在讀取包含外部 DTD 的 XML 檔案,它會呼叫 XmlUrlResolver.GetEntityAsync 方法來取得 DTD 的資料流程表示。If an XML reader (XmlReader) is reading an XML file that contains an external DTD, it calls the XmlUrlResolver.GetEntityAsync method to get a stream representation of the DTD. 如果 DTD 的 URI 是相對 URI,則 XML 讀取器會呼叫 XmlUrlResolver.ResolveUri 方法,並傳回給定 relativeUribaseURi 參數的絕對 URI。If the URI of the DTD is a relative URI, the XML reader calls the XmlUrlResolver.ResolveUri method and returns an absolute URI for the given relativeUri and baseURi parameters. 如果 XmlUrlResolver 不知道如何解析 URI,它會傳回 nullIf the XmlUrlResolver doesn't know how to resolve the URI, it returns null.

XmlUrlResolver.GetEntity 方法會適當地使用 Credentials 屬性中的資訊來取得資源的存取。The XmlUrlResolver.GetEntity method uses the information in the Credentials property as appropriate to gain access to the resource. 由於安全性原因,沒有針對此屬性的 get 存取子。There is no get accessor to this property for security reasons. 當覆寫 XmlResolver時, GetEntity是利用認證屬性中認證資訊的方法。When overwriting XmlResolver, GetEntity is the method that utilizes the credential information in the Credentials property.

解析所有其他 XML 資源與解析 DTD 很相似。Resolving all other XML resources is very similar to resolving DTDs. XmlResolver 會協調與外部資源的連接,並傳回內容的 Stream 表示。XmlResolver negotiates the connection with the external resource and returns a Stream representation of the content. XmlResolver 進行呼叫的物件會解讀資料流程。The object that is making the call to XmlResolver interprets the stream.

擴充 XmlUrlResolver 類別Extending the XmlUrlResolver class

XmlUrlResolver 類別的預設行為是解析它的來源 (而非快取) 中的 XML 資料資源。The default behavior of the XmlUrlResolver class is to resolve an XML data resource from its source, not from cache. 在某些情況下,解析快取中的資料資源可提高應用程式的效能,因為這樣會節省資料資源伺服器的往來成本。In some cases, resolving a data resource from cache can improve the performance of an application by saving a trip to the data resource's server. 這裡得到的效能必須針對最新內容的需求來加以考量。The performance gains here must be weighed against the need for up-to-date content.

下列範例會擴充 XmlUrlResolver,並建立新的類別 XmlCachingResolver,以從快取中取出資源。The following example extends XmlUrlResolver and builds a new class, XmlCachingResolver, to retrieve resources from the cache. 這是透過覆寫 XmlUrlResolver.Credentials 屬性和 XmlUrlResolver.GetEntity 方法來完成。This is done by overriding the XmlUrlResolver.Credentials property and the XmlUrlResolver.GetEntity method.

class XmlCachingResolver : XmlUrlResolver
{
    bool enableHttpCaching;
    ICredentials credentials;

    //resolve resources from cache (if possible) when enableHttpCaching is set to true
    //resolve resources from source when enableHttpcaching is set to false 
    public XmlCachingResolver(bool enableHttpCaching)
    {
        this.enableHttpCaching = enableHttpCaching;
    }

    public override ICredentials Credentials
    {
        set
        {
            credentials = value;
            base.Credentials = value;
        }
    }

    public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
    {
        if (absoluteUri == null)
        {
            throw new ArgumentNullException("absoluteUri");
        }
        //resolve resources from cache (if possible)
        if (absoluteUri.Scheme == "http" && enableHttpCaching && (ofObjectToReturn == null || ofObjectToReturn == typeof(Stream)))
        {
            WebRequest webReq = WebRequest.Create(absoluteUri);
            webReq.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
            if (credentials != null)
            {
                webReq.Credentials = credentials;
            }
            WebResponse resp = webReq.GetResponse();
            return resp.GetResponseStream();
        }
        //otherwise use the default behavior of the XmlUrlResolver class (resolve resources from source)
        else
        {
            return base.GetEntity(absoluteUri, role, ofObjectToReturn);
        }
    }
}
Class XmlCachingResolver
    Inherits XmlUrlResolver
    Dim enableHttpCaching As Boolean
    Public Shadows Credentials As ICredentials

    'resolve resources from cache (if possible) when enableHttpCaching is set to true
    'resolve resources from source when enableHttpcaching is set to false
    Public Sub New(ByVal enableHttpCaching As Boolean)
        Me.enableHttpCaching = enableHttpCaching
    End Sub

    Public Shadows Function GetEntity(ByVal absoluteUri As Uri, ByVal role As String, ByVal returnType As Type) As Object
        If absoluteUri = Nothing Then
            Throw New ArgumentNullException("absoluteUri")
        End If

        'resolve resources from cache (if possible)
        If absoluteUri.Scheme = "http" And enableHttpCaching And (returnType Is GetType(Nullable) Or returnType Is GetType(Stream)) Then
            Dim webReq As WebRequest = WebRequest.Create(absoluteUri)
            webReq.CachePolicy = New HttpRequestCachePolicy(HttpRequestCacheLevel.Default)
            If Not (Credentials Is Nothing) Then
                webReq.Credentials = Credentials
            End If
            Dim resp As WebResponse = webReq.GetResponse()
            Return resp.GetResponseStream()
            'otherwise use the default behavior of the XmlUrlResolver class (resolve resources from source)
        Else
            Return MyBase.GetEntity(absoluteUri, role, returnType)
        End If

    End Function
End Class

XmlCachingResolver 類別的快取行為是在 GetEntity 方法中實作。The caching behavior of the XmlCachingResolver class is implemented in the GetEntity method. 這是透過建立新的 WebRequestHttpRequestCachePolicy 物件來完成。This is done by creating new WebRequest and HttpRequestCachePolicy objects. HttpRequestCachePolicy 物件是使用 Default 列舉的 HttpRequestCacheLevel 成員所建立。The HttpRequestCachePolicy object is created using the Default member of the HttpRequestCacheLevel enumeration.

CachePolicy 物件的 WebRequest 屬性是使用 HttpRequestCachePolicy 物件所設定。The CachePolicy property of the WebRequest object is set with the HttpRequestCachePolicy object.

系統會使用 Boolean enableHttpCaching來建立 XmlCachingResolver 類別的實例。An instance of the XmlCachingResolver class is created with the Boolean enableHttpCaching. 當此值設定為 true時,實例會盡可能解析預設快取中的資源。When this value is set to true, the instance resolves a resource from the default cache if possible. enableHttpCaching 設定為 false時,實例會使用預設行為,並解析其來源的資源。When enableHttpCaching is set to false, the instance uses the default behavior and resolves resources from their source.

注意

這個範例會利用 .NET Framework 中 XML 類別的擴充性。This example leverages the extensibility of the XML classes in the .NET Framework. 其他類別可加以擴充和自訂,以符合特定應用程式的需要。Other classes can be extended and customized to suit the needs of a particular application.

建構函式

XmlUrlResolver()

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

屬性

CachePolicy

取得或設定基礎 WebRequest 物件的快取原則。Gets or sets the cache policy for the underlying WebRequest object.

Credentials

設定用以驗證 Web 要求的認證資訊。Sets credentials used to authenticate web requests.

Proxy

取得或設定基礎 WebRequest 物件的網路 Proxy。Gets or sets the network proxy for the underlying WebRequest object.

方法

Equals(Object)

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

(繼承來源 Object)
GetEntity(Uri, String, Type)

對應 URI 至包含實際資源的物件。Maps a URI to an object that contains the actual resource.

GetEntityAsync(Uri, String, Type)

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

GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

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

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ResolveUri(Uri, String)

從基底和相對 URI 解析絕對 URI。Resolves the absolute URI from the base and relative URIs.

SupportsType(Uri, Type)

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

(繼承來源 XmlResolver)
ToString()

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

(繼承來源 Object)

適用於

另請參閱