Export (0) Print
Expand All

XmlTextReader.XmlResolver Property

Sets the XmlResolver used for resolving DTD references.

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

public XmlResolver XmlResolver { set; }

Property Value

Type: System.Xml.XmlResolver
The XmlResolver to use. If set to null, 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.

NoteNote:

In the .NET Framework version 2.0 release, the recommended practice is to create XmlReader instances using the XmlReader.Create method. This allows you to take full advantage of the new features introduced in this release. For more information, see Creating XML Readers.

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 null, 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 null. External resources are not resolved.

The following example uses the XmlResolver property to specify the credentials necessary to access the networked file.

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

  }
} 
#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();     

}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft