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 */
public void set_XmlResolver (XmlResolver value)

public function set XmlResolver (value : 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 to specify an XmlResolver.

NoteNote

The XmlValidatingReader class is obsolete in Microsoft .NET Framework version 2.0. You can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. For more information, see Validating XML Data with XmlReader.

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 a null reference (Nothing in Visual Basic), 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 a null reference (Nothing in Visual Basic). 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">

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show: