Export (0) Print
Expand All

XmlValidatingReader.XmlResolver Property

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.

NoteNote

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);
       break;
     case XmlNodeType.Text:
       Console.Write(reader.Value);
       break;
     case XmlNodeType.DocumentType:
       Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value);
       break;
     case XmlNodeType.EntityReference:
       Console.Write(reader.Name);
       break;
     case XmlNodeType.EndElement:
       Console.Write("</{0}>", reader.Name);
       break;
  }        
}           

The example uses the following files as input.

book5.xml

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

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">

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft