System.Xml.XmlUrlResolver Class

Assembly: System.Xml.dll
Namespace: System.Xml
Summary
Resolves external XML resources named by a URI.
C# Syntax:
public class XmlUrlResolver : XmlResolver
Remarks
XmlUrlResolver is used to resolve external XML resources such as entities, DTDs or schemas. It is also used to process include and import elements found in Extensible StyleSheet Language (XSL) stylesheets or XML Schema Definition language (XSD) schemas.

XmlUrlResolver is the default resolver for all classes in the System.Xml namespace. However, you can also create your own resolver. For more information on implementing your own resolver, see the conceptual topic at MSDN: creatingcustomresolver.

See also:
System.Xml Namespace

System.Xml.XmlUrlResolver Member List:

Public Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Creates a new instance of the XmlUrlResolver class.
Public Properties
Credentials Write-only

Overridden:
Sets credentials used to authenticate Web requests.
Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetEntity Overridden:
Maps a URI to an object containing the actual resource.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ResolveUri Overridden:
Resolves the absolute URI from the base and relative URIs.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Xml.XmlUrlResolver Member Details

ctor #1
Summary
Creates a new instance of the XmlUrlResolver class.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public XmlUrlResolver();

Return to top


Overridden Property: Credentials (write-only)
Summary
Sets credentials used to authenticate Web requests.
C# Syntax:
ICredentials Credentials {set;}
Remarks
If the virtual directory is configured to allow anonymous access, this property does not need to be set. Otherwise, the credentials of the user must be supplied. If credentials are needed but not supplied, XmlUrlResolver uses default credentials (CredentialCache.DefaultCredentials).

The following C# code sets credentials for the virtual directory "http://localhost/bookstore/inventory.xml"

              XmlTextReader reader = new XmlTextReader("http://localhost/bookstore/inventory.xml");
              NetworkCredential nc = new NetWorkCredential("username", "password", "domain");
              XmlUrlResolver resolver = new XmlUrlResolver();
              resolver.Credentials = nc;
              reader.XmlResolver= resolver;
                 
            

Different credentials can be associated with different URIs, and added to a credential cache. The credentials can then be used to check authentication for different URIs regardless of the original source of the XML.

              NetworkCredential myCred = new NetworkCredential("username","password","domain"); 
              CredentialCache myCache = new CredentialCache(); 
              myCache.Add(new Uri("http://www.contoso.com/"), "Basic", myCred); 
              myCache.Add(new Uri("http://app.contoso.com/"), "Basic", myCred);
              XmlUrlResolver resolver = new XmlUrlResolver();
              resolver.Credentials = myCache;
              reader.XmlResolver = resolver;
                 
            
Example
The following example parses a local XML file that includes a reference to an external DTD file that is stored on a network server. The XmlUrlResolver is used to specify the credentials necessary to access the DTD file.
using System;
using System.IO;
using System.Xml;
using System.Net;

public class Sample 
{
  public static void Main(){

    //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();
    NetworkCredential nc = new NetworkCredential("username","password","domain");
    resolver.Credentials = nc;
    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();       
  }
} // End class

    

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

    

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~XmlUrlResolver();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: GetEntity(
   Uri absoluteUri,
   string role,
   Type ofObjectToReturn
)
Summary
Maps a URI to an object containing the actual resource.
C# Syntax:
public override object GetEntity(
   Uri absoluteUri,
   string role,
   Type ofObjectToReturn
);
Parameters:

absoluteUri

The URI returned from XmlUrlResolver.ResolveUri

role

The current implementation does not use this parameter when resolving URIs. This is provided for future extensibility purposes. For example, this can be mapped to the xlink:role and used as an implementation specific argument in other scenarios.

ofObjectToReturn

The type of object to return. The current implementation only returns System.IO.Stream objects.

Return Value:
A System.IO.Stream object or null if a type other than stream is specified.
Exceptions
Exception Type Condition
XmlException There is a runtime error (for example, an interrupted server connection)
UriFormatException The specified URI is not an absolute URI.
Remarks
This method is used when the caller wants to map a given URI into an object containing the actual resource that the URI represents.
Example
The following example demonstrates the GetEntity and XmlUrlResolver.ResolveUri methods.
using System;
using System.IO;
using System.Xml;
 
 public class Sample
 {
          
   public static void Main()
   {
       XmlUrlResolver resolver = new XmlUrlResolver();
 
       Uri baseUri = new Uri ("http://servername/tmp/test.xsl");
 
       Uri fulluri=resolver.ResolveUri(baseUri, "includefile.xsl");
 
       // Get a stream object containing the XSL file
       Stream s=(Stream)resolver.GetEntity(fulluri, null, typeof(Stream));
 
       // Read the stream object displaying the contents of the XSL file
       XmlTextReader reader = new XmlTextReader(s);
       while (reader.Read()) 
       {
          Console.WriteLine(reader.ReadOuterXml());
       } 
   }
}

    
See also:
Uri

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: ResolveUri(
   Uri baseUri,
   string relativeUri
)
Summary
Resolves the absolute URI from the base and relative URIs.
C# Syntax:
public override Uri ResolveUri(
   Uri baseUri,
   string relativeUri
);
Parameters:

baseUri

The base URI used to resolve the relative URI

relativeUri

The URI to resolve. The URI can be absolute or relative. If absolute, this value effectively replaces the baseUri value. If relative, it combines with the baseUri to make an absolute URI. The URI to resolve. The URI can be absolute or relative. If absolute, this value effectively replaces the baseUri value. If relative, it combines with the baseUri to make an absolute URI.

Return Value:
The absolute URI or null if the relative URI can not be resolved.
Exceptions
Exception Type Condition
UriFormatException The supplied URIs are not absolute.
Remarks
The absolute URI may be used as the base URI for any subsequent requests for entities that are relative to this URI.
Example
The following example demonstrates the XmlUrlResolver.GetEntity and ResolveUri methods.
using System;
using System.IO;
using System.Xml;
 
 public class Sample
 {
          
   public static void Main()
   {
       XmlUrlResolver resolver = new XmlUrlResolver();
 
       Uri baseUri = new Uri ("http://servername/tmp/test.xsl");
 
       Uri fulluri=resolver.ResolveUri(baseUri, "includefile.xsl");
 
       // Get a stream object containing the XSL file
       Stream s=(Stream)resolver.GetEntity(fulluri, null, typeof(Stream));
 
       // Read the stream object displaying the contents of the XSL file
       XmlTextReader reader = new XmlTextReader(s);
       while (reader.Read()) 
       {
          Console.WriteLine(reader.ReadOuterXml());
       } 
   }
}

    
See also:
Uri

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.