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

示例

下面的示例创建一个 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.

XmlUrlResolver 命名空间中所有类的默认解析程序 System.XmlXmlUrlResolver 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 方法并返回给定和参数的绝对 URI relativeUri baseURiIf 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. 当覆盖时 XmlResolverGetEntity 是使用 凭据 属性中的凭据信息的方法。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 enableHttpCachingAn 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 对象的网络代理。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.

GetEntityAsync(Uri, String, Type)

将 URI 异步映射到包含实际资源的对象。Asynchronously maps a URI to an object that contains the actual resource.

(继承自 XmlResolver)
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 解析绝对 URI。Resolves the absolute URI from the base and relative URIs.

ResolveUri(Uri, String)

当在派生类中被重写时,从基 URI 和相对 URI 解析绝对 URI。When overridden in a derived class, resolves the absolute URI from the base and relative URIs.

(继承自 XmlResolver)
SupportsType(Uri, Type)

使解决程序能够返回 Stream 以外的类型。Enables the resolver to return types other than Stream.

(继承自 XmlResolver)
ToString()

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

(继承自 Object)

适用于

另请参阅