Accessing External Resources for the XmlReader

The XmlResolver class is used to locate and access any resources required by the XmlReader object.

Overview

The XmlResolver can be used to do the following:

  • Locate and open the XML instance document.

  • Locate and open any external resources referenced by the XML instance document. This can include entities, a document type definition, schemas, and so on.

  • If the resource is stored on a system that requires authentication, the XmlResolver.Credentials property can be used to specify the necessary credentials.

You specify the XmlResolver to use by setting the XmlReaderSettings.XmlResolver property and passing the XmlReaderSettings object to the Create method.

Note

If an XmlResolver is not specified, the created reader uses a default XmlUrlResolver with no user credentials.

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 );
// Create a resolver with default credentials.
XmlUrlResolver* resolver = new XmlUrlResolver();
resolver->Credentials = System::Net::CredentialCache::DefaultCredentials;

// Create the XmlReader object.
XmlReader* reader = XmlReader::Create(S"https://ServerName/data/books.xml", 0, resolver, settings);

The System.Xml namespace includes two concrete implementations of the XmlResolver class.

For additional information, see Resolve External XML Resources Named by a URI.

See Also

Concepts

Reading XML with the XmlReader

Other Resources

Using the XmlReader Class

Security and Your System.Xml Applications