Breyta

Deila með


XmlValidatingReader.XmlResolver Property

Definition

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.

public:
 property System::Xml::XmlResolver ^ XmlResolver {  void set(System::Xml::XmlResolver ^ value); };
public System.Xml.XmlResolver XmlResolver { set; }
member this.XmlResolver : System.Xml.XmlResolver
Public Property XmlResolver As XmlResolver

Property Value

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.

Examples

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

   // Create the reader. -> 
   XmlTextReader^ txtreader = gcnew XmlTextReader( "book5.xml" );
   XmlValidatingReader^ reader = gcnew XmlValidatingReader( txtreader );
   txtreader->WhitespaceHandling = WhitespaceHandling::None;
   
   // Set the credentials necessary to access the DTD file stored on the network.
   XmlUrlResolver^ resolver = gcnew 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;
      }
   }

   
   // Close the reader.
   reader->Close();
}

// 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;
  }
}
' Create the reader.
Dim txtreader as XmlTextReader = new XmlTextReader("book5.xml")
Dim reader as XmlValidatingReader = new XmlValidatingReader(txtreader)
txtreader.WhitespaceHandling = WhitespaceHandling.None

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

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

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

Remarks

Note

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.

Applies to

See also