Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
XmlUrlResolver Class
Collapse the table of content
Expand the table of content

XmlUrlResolver Class

Resolves external XML resources named by a Uniform Resource Identifier (URI).

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

public class XmlUrlResolver : XmlResolver

The XmlUrlResolver type exposes the following members.

Public methodSupported by the XNA FrameworkXmlUrlResolverInitializes a new instance of the XmlUrlResolver class.

Public propertyCachePolicyGets or sets the cache policy for the underlying WebRequest object.
Public propertySupported by the XNA FrameworkCredentialsSets credentials used to authenticate web requests. (Overrides XmlResolver.Credentials.)
Public propertySupported by the XNA FrameworkNameTable Obsolete. Gets the XmlNameTable associated with this implementation. (Inherited from XmlResolver.)
Public propertyProxyGets or sets the network proxy for the underlying WebRequest object.

Public methodSupported by the XNA FrameworkEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetEntityMaps a URI to an object that contains the actual resource. (Overrides XmlResolver.GetEntity(Uri, String, Type).)
Public methodGetEntityAsyncAsynchronously maps a URI to an object that contains the actual resource. (Overrides XmlResolver.GetEntityAsync(Uri, String, Type).)
Public methodSupported by the XNA FrameworkGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkResolveUriResolves the absolute URI from the base and relative URIs. (Overrides XmlResolver.ResolveUri(Uri, String).)
Public methodSupportsTypeEnables the resolver to return types other than System.IO.Stream. (Inherited from XmlResolver.)
Public methodSupported by the XNA FrameworkToStringReturns a string that represents the current object. (Inherited from Object.)

XmlUrlResolver 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.

XmlUrlResolver is the default resolver for all classes in the System.Xml namespace. It supports the file:// and http:// protocols and requests from the WebRequest class.

Security noteSecurity Note

XmlUrlResolver objects can contain sensitive information such as user credentials. You should be careful when you cache XmlUrlResolver objects and should not pass XmlUrlResolver objects to an untrusted component.

Resolving DTDs

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 XmlUrlResolver doesn't know how to resolve the URI, it returns null.

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 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.

The following example extends XmlUrlResolver 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 : 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
            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) 
            return base.GetEntity(absoluteUri, role, ofObjectToReturn);

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.

The CachePolicy property of the WebRequest object is set with the HttpRequestCachePolicy object.

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 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);

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
© 2015 Microsoft