XmlUrlResolver XmlUrlResolver XmlUrlResolver XmlUrlResolver Class

定义

解析由统一资源标识符 (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
继承
XmlUrlResolverXmlUrlResolverXmlUrlResolverXmlUrlResolver

示例

下面的示例创建一个XmlReader , 它将XmlUrlResolver与默认凭据一起使用。The 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) 架构中找到的包含和导入元素。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. 如果不知道如何解析 URI, 它将返回nullXmlUrlResolverIf 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.

使用 XmlCachingResolver Boolean 创建 enableHttpCaching 类的一个实例。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() XmlUrlResolver() XmlUrlResolver()

初始化 XmlUrlResolver 类的新实例。Initializes a new instance of the XmlUrlResolver class.

属性

CachePolicy CachePolicy CachePolicy CachePolicy

获取或设置 WebRequest 基础对象的缓存策略。Gets or sets the cache policy for the underlying WebRequest object.

Credentials Credentials Credentials Credentials

设置用于对 Web 请求进行身份验证的凭据。Sets credentials used to authenticate web requests.

Proxy Proxy Proxy Proxy

获取或设置 WebRequest 基础对象的网络代理。Gets or sets the network proxy for the underlying WebRequest object.

方法

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 映射到包含实际资源的对象。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 的浅表副本。Creates a shallow copy of the current Object.

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

从基 URI 和相对 URI 解析绝对 URI。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.

(Inherited from XmlResolver)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于

另请参阅