Resolving Resources Using the XmlResolver

The XmlReader class uses XmlResolver to resolve external DTDs, entities, and schemas. The XmlReader class uses an XmlUrlResolver with no user credentials by default. You specify the XmlResolver to use by setting the XmlResolver property and passing the XmlReaderSettings object to the Create method.

The following code creates an XmlReader instance that uses an XmlUrlResolver with default credentials.

' Create a resolver with default credentials.
Dim resolver as XmlUrlResolver = new XmlUrlResolver()
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials

' Set the reader settings object to use the resolver.
settings.XmlResolver = resolver

' Create the XmlReader object.
Dim reader as XmlReader = XmlReader.Create("https://ServerName/data/books.xml", settings)
// Create a resolver with default credentials.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Set the reader settings object to use the resolver.
settings.XmlResolver = resolver;

// Create the XmlReader object.
XmlReader reader = XmlReader.Create("https://ServerName/data/books.xml", settings);
// Create a resolver with default credentials.
XmlUrlResolver^ resolver = gcnew XmlUrlResolver;
resolver->Credentials = System::Net::CredentialCache::DefaultCredentials;

 // Set the reader settings object to use the resolver.
 settings->XmlResolver = resolver;

// Create the XmlReader object.
XmlReader^ reader = XmlReader::Create( L"https://ServerName/data/books.xml", settings );

If the books.xml file contained an external DTD, the XmlReader handles it in the following manner. To resolve a DTD, XmlReader calls the GetEntity method to get a stream representation of the entity. If the URI of the DTD is a relative URI, then XmlReader calls the ResolveUri method and returns an absolute URI for the given relativeUri and baseUri. If XmlUrlResolver does not know how to resolve the given URI, then it returns a null reference.

The 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 needs to know only how to negotiate the connection with the external resource, and return a Stream representation of the content. It is the object that is making the call to XmlResolver that has the task of interpreting the stream.

See Also

Concepts

Supplying Authentication Credentials to XmlResolver when Reading from a File

Accessing External Resources for the XmlReader

Resolving External Resources During XSLT Processing

Other Resources

Resolve External XML Resources Named by a URI