Resolves external XML resources named by a Uniform Resource Identifier (URI).
Assembly: System.Xml (in System.Xml.dll)
Thetype exposes the following members.
|CachePolicy||Gets or sets the cache policy for the underlying WebRequest object.|
|Credentials||Sets credentials used to authenticate web requests. (Overrides XmlResolver.Credentials.)|
|NameTable||Obsolete. Gets the XmlNameTable associated with this implementation. (Inherited from XmlResolver.)|
|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. (Inherited from Object.)|
|GetEntity||Maps a URI to an object that contains the actual resource. (Overrides XmlResolver.GetEntity(Uri, String, Type).)|
|GetEntityAsync||Asynchronously maps a URI to an object that contains the actual resource. (Overrides XmlResolver.GetEntityAsync(Uri, String, Type).)|
|GetHashCode||Serves as the default hash function. (Inherited from Object.)|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|ResolveUri||Resolves the absolute URI from the base and relative URIs. (Overrides XmlResolver.ResolveUri(Uri, String).)|
|SupportsType||Enables the resolver to return types other than System.IO.Stream. (Inherited from XmlResolver.)|
|ToString||Returns a string that represents the current object. (Inherited from Object.)|
is used to resolve external XML resources such as entities, document type definitions (DTDs) or schemas. 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.
objects can contain sensitive information such as user credentials. You should be careful when you cache objects and should not pass objects to an untrusted component.
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. 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. If the doesn't know how to resolve the URI, it returns Nothing.
The XmlUrlResolver.GetEntity method uses the information in the Credentials property as appropriate to gain access to the resource. There is no get accessor to this property for security reasons. When overwriting XmlResolver, GetEntity is the method that utilizes the credential information in the Credentials property.
Resolving all other XML resources is very similar to resolving DTDs. XmlResolver negotiates the connection with the external resource and returns a Stream representation of the content. The object that is making the call to XmlResolver interprets the stream.
Extending the XmlUrlResolver class
The default behavior of the 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.
The following example extends and builds a new class, XmlCachingResolver, to retrieve resources from the cache. This is done by overriding the XmlUrlResolver.Credentials property and the XmlUrlResolver.GetEntity method.
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
The caching behavior of the XmlCachingResolver class is implemented in the GetEntity method. This is done by creating new WebRequest and HttpRequestCachePolicy objects. The HttpRequestCachePolicy object is created using the Default member of the HttpRequestCacheLevel enumeration.
An instance of the XmlCachingResolver class is created with the Boolean enableHttpCaching. When this value is set to true, the instance resolves a resource from the default cache if possible. When enableHttpCaching is set to false, the instance uses the default behavior and resolves resources from their source.
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.
The following example creates an XmlReader that uses an with default credentials.
' 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)