XmlValidatingReader.XmlResolver Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Sets the XmlResolver used for resolving external document type definition (DTD) and schema location references. The XmlResolver is also used to handle any import or include elements found in XML Schema definition language (XSD) schemas.

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 to specify an XmlResolver.


The XmlValidatingReader class is obsolete in .NET Framework 2.0. You can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. For more information, see the Remarks section of the XmlReader reference page.

The XmlResolver is used to load any DTDs, entities, or schemas needed to complete the validation process.

This property can be set at any time and takes affect after the next Read call. If this property is set to null, the reader assumes the user is not interested in resolving external references. In this case, the reader only validates against internal resources, if the resource is present.

In version 1.1 of .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.

When validating using schemas, you can avoid the expensive load process by providing an XmlSchemaCollection using the Schemas property.

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

// Create the reader.
XmlTextReader txtreader = new XmlTextReader("book5.xml");
XmlValidatingReader reader = new XmlValidatingReader(txtreader);
txtreader.WhitespaceHandling = WhitespaceHandling.None;

// Set the credentials necessary to access the DTD file stored on the network.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
reader.XmlResolver = resolver;

// Display each of the element nodes.
while (reader.Read()){
   switch (reader.NodeType){
     case XmlNodeType.Element:
       Console.Write("<{0}>", reader.Name);
     case XmlNodeType.Text:
     case XmlNodeType.DocumentType:
       Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value);
     case XmlNodeType.EntityReference:
     case XmlNodeType.EndElement:
       Console.Write("</{0}>", reader.Name);

The example uses the following files as input.


<!DOCTYPE book SYSTEM 'http://myServer/DTDs/books.dtd'>
<book ISBN = '1-861001-57-5'>
  <title>Pride And Prejudice</title>


.NET Framework
Available since 1.1
Return to top