public class XmlUrlResolver : XmlResolver
|
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.
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. |
Credentials | Write-only Overridden: Sets credentials used to authenticate Web requests. |
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. |
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:
public XmlUrlResolver(); |
ICredentials Credentials {set;}
|
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;
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">
~XmlUrlResolver(); |
absoluteUri
role
ofObjectToReturn
Exception Type | Condition |
---|---|
XmlException | There is a runtime error (for example, an interrupted server connection) |
UriFormatException | The specified URI is not an absolute URI. |
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()); } } }
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
baseUri
relativeUri
Exception Type | Condition |
---|---|
UriFormatException | The supplied URIs are not absolute. |
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()); } } }
public virtual string ToString(); |