Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

XmlDocument.XmlResolver Property

Sets the XmlResolver to use for resolving external resources.

[Visual Basic]
Property XmlResolver As XmlResolver
[C#]
XmlResolver XmlResolver {set;}
[C++]
public: __property virtual void set_XmlResolver(XmlResolver*);
[JScript]
public function set XmlResolver(XmlResolver);

Property Value

The XmlResolver to use.

In version 1.1 of the .NET Framework, the caller must be fully-trusted in order to specify an XmlResolver.

Exceptions

Exception Type Condition
XmlException This property is set to a null reference (Nothing in Visual Basic) and an external DTD or entity is encountered.

Remarks

The XmlResolver can be used to load DTDs or expand entity references. Using the XmlResolver.Credentials property, you can set credentials on the XmlResolver to access resources stored on a secure network resource.

  • If the document was not loaded using an XmlReader (that is, if it was loaded using a stream, file, and so on) the XmlResolver on the XmlDocument is always used.
  • If the document was loaded with an XmlTextReader, the resolver on the XmlTextReader is used to resolve any DTD references in the DocumentType node. The resolver on the XmlDocument is used to expand any entity references.
  • If the document was loaded with an XmlValidatingReader, the resolver on the XmlDocument is never used.
  • If the document was loaded with a class that extends XmlReader and the XmlReader cannot resolve entities (CanResolveEntity returns false), the XmlResolver on the XmlDocument is used to resolve any references in the DocumentType node and to expand any entity references.
Note   If the XmlDocument is loaded using an XmlReader which had an XmlResolver set to it, the XmlResolver on the XmlReader is not cached by the XmlDocument after Load completes.

In version 1.1 of the .NET Framework, if this property is not set, the trust level of the application determines the default behavior.

Fully-trusted code: The document uses a default XmlUrlResolver with no user credentials. If authentication is required to access a network resource, use the XmlResolver property to specify an XmlResolver with the necessary credentials.

Semi-trusted code: The XmlResolver property is set to a null reference (Nothing in Visual Basic). External resources are not resolved.

For more information on security and the XmlResolver property, see Resolving External Resources.

This property is a Microsoft extension to the Document Object Model (DOM).

Example

[Visual Basic, C#, C++] The following example loads an XML document which includes a reference to a DTD file. The XmlResolver property is used to set the credentials necessary to access the network resource.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml
Imports System.Net

public class Sample 

  public shared sub Main()

    ' Supply the credentials necessary access the DTD file stored on the network.
    Dim resolver as XmlUrlResolver = new XmlUrlResolver()
    resolver.Credentials = CredentialCache.DefaultCredentials

    ' Create and load the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    doc.XmlResolver = resolver  ' Set the resolver.
    doc.Load("book5.xml")

    ' Display the entity replacement text which is pulled from the DTD file.
    Console.WriteLine(doc.DocumentElement.LastChild.InnerText)
  
  end sub
end class

[C#] 
using System;
using System.IO;
using System.Xml;
using System.Net;

public class Sample {

  public static void Main() {

    // Supply the credentials necessary to access the DTD file stored on the network.
    XmlUrlResolver resolver = new XmlUrlResolver();
    resolver.Credentials = CredentialCache.DefaultCredentials;

    // Create and load the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.XmlResolver = resolver;  // Set the resolver.
    doc.Load("book5.xml");

    // Display the entity replacement text which is pulled from the DTD file.
    Console.WriteLine(doc.DocumentElement.LastChild.InnerText);
  
  }
} // End class

[C++] 
#using <mscorlib.dll>
#using <System.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Net;

int main() {

    // Supply the credentials necessary to access the DTD file stored on the network.
    XmlUrlResolver* resolver = new XmlUrlResolver();
    resolver->Credentials = CredentialCache::DefaultCredentials;

    // Create and load the XmlDocument.
    XmlDocument* doc = new XmlDocument();
    doc->XmlResolver = resolver;  // Set the resolver.
    doc->Load(S"book5.xml");

    // Display the entity replacement text which is pulled from the DTD file.
    Console::WriteLine(doc->DocumentElement->LastChild->InnerText);
  
}

[Visual Basic, C#, C++] The example uses the following data files as input.

[Visual Basic, C#, C++] book5.xml

<!DOCTYPE book SYSTEM 'http://myServer/data/books.dtd'>
<book ISBN = '1-861001-57-5'>
  <title>Oberon's Legacy</title>
  <price>19.95</price>
  <misc>&h;</misc>
</book>

[Visual Basic, C#, C++] books.dtd

<!ELEMENT book (title,price,misc)> 
<!ATTLIST book 
   genre CDATA "novel"
   ISBN CDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT misc (#PCDATA)>
<!ENTITY h "hardcover">
<!ENTITY p "paperback">

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

XmlDocument Class | XmlDocument Members | System.Xml Namespace | Credentials | System.Net.CredentialCache | System.Net.NetworkCredential | XmlSecureResolver

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft