This topic has not yet been rated - Rate this topic

XmlTextReader.XmlResolver Property

Sets the XmlResolver used for resolving DTD references.

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

Property Value

The XmlResolver to use. If set to a null reference (Nothing in Visual Basic), external resources are not resolved.

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

Remarks

The reader uses XmlResolver to resolve the location of the file loaded into the reader and also to resolve DTD references. For example, if your XML included the DOCTYPE declaration, <!DOCTYPE book SYSTEM book.dtd> the reader resolves this external file and ensures that the DTD is well-formed. The reader does not use the DTD for validation.

This property can be changed at any time and takes effect on the next read operation. If this property is set to a null reference (Nothing in Visual Basic), any external DTD references encountered by the reader are not resolved.

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 reader 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). External resources are not resolved.

Example

[Visual Basic, C#, C++] The following example uses the XmlResolver property to specify the credentials necessary to access the networked file.

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

public class Sample 

  public shared sub Main() 

    ' Create the reader.
    Dim reader as XmlTextReader = new XmlTextReader("http://myServer/data/books.xml")
   
    ' Supply the credentials necessary to access the Web server.
    Dim resolver as XmlUrlResolver = new XmlUrlResolver()
    resolver.Credentials = CredentialCache.DefaultCredentials
    reader.XmlResolver = resolver

    ' Parse the file.
    while (reader.Read()) 
       ' Do any additional processing here.
    end while           
  
    ' Close the reader.
    reader.Close()     
  
  end sub
end class

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

public class Sample {

  public static void Main() {

    // Create the reader.
    XmlTextReader reader = new XmlTextReader("http://myServer/data/books.xml");
   
    // Supply the credentials necessary to access the Web server.
    XmlUrlResolver resolver = new XmlUrlResolver();
    resolver.Credentials = CredentialCache.DefaultCredentials;
    reader.XmlResolver = resolver;

    // Parse the file.
    while (reader.Read()) {
       // Do any additional processing here.
    }           
  
    // Close the reader.
    reader.Close();     
  
  }
} 

[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() {

    // Create the reader.
    XmlTextReader* reader = new XmlTextReader(S"http://myServer/data/books.xml");
   
    // Supply the credentials necessary to access the Web server.
    XmlUrlResolver* resolver = new XmlUrlResolver();
    resolver->Credentials = CredentialCache::DefaultCredentials;
    reader->XmlResolver = resolver;

    // Parse the file.
    while (reader->Read()) {
       // Do any additional processing here.
    }           
  
    // Close the reader.
    reader->Close();     
  
}

[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

XmlTextReader Class | XmlTextReader Members | System.Xml Namespace | XmlUrlResolver.Credentials | CredentialCache | NetworkCredential | XmlSecureResolver

Did you find this helpful?
(1500 characters remaining)