Loading XML Data into an XmlTextReader


Loading XML Data into an XmlTextReader

The first thing to do when parsing an XML document with an XmlTextReader is to load the stream into the reader. To do this, you simply need to create an instance of the XmlTextReader and tell it where it can find the document. The .NET Compact Framework provides several overloaded constructors for loading an XmlTextReader from different sources.

Loading an XML Stream from the Local File System

The easiest way to create an instance of the XmlTextReader is to call the constructor that takes a string representing the URI to where the XML document can be found, as shown in Listing 10.1.

Listing 10.1
 C# XmlTextReader reader = new XmlTextReader("filename.xml"); VB Dim reader As New XmlTextReader("filename.xml") 

When passing a filename to this constructor, it is important to note that Windows CE does not have the concept of a current directory. This means that relative paths cannot be constructed without some interesting and tricky code. In fact, calling Directory.GetCurrentDirectory will raise a NotSupportedException . Calling the XmlTextReader constructor with a relative filepath will raise a FileNotFoundException unless the file exists in the root directory. Therefore, it is recommended that you always specify the fully qualified path of the file when creating an XmlTextReader .

Loading an XML File from a Remote Location

An XmlTextReader can also be created that is bound to an XML file that resides on a remote machine. This can be accomplished by passing the URL to the XmlTextReader constructor, as seen in Listing 10.2.

Listing 10.2
 C# XmlTextReader xmlReader = new XmlTextReader("http://foo.com/bar.xml"); VB Dim reader As New XmlTextReader("http://foo.com/bar.xml") 

This constructor creates an XmlTextReader object that parses an XML file located at http://foo.com/bar.xml. The XmlTextReader will take care of creating a request to the server, opening a stream to the URL, and reading the XML data from the request. You need to interact with only the XmlTextReader .

In the real world, your XML data may not be anonymously accessible on your server. You may want to protect your data by applying some authentication scheme to the Web access. If your XML data is password-protected with Digest or Basic Authentication, the .NET Compact Framework can easily empower you to load an XmlTextReader with this protected data. Windows Authentication is not supported by the .NET Framework. Listing 10.3 exemplifies how to load an XmlTextReader from a password-protected Web server.

Listing 10.3
 C# static void Main() {   NetworkCredential cred =     new NetworkCredential("usrnm", "psswd", "domain");   Stream s =     GetDocumentStream("http://www/foo.com/bar.xml", cred,);   XmlTextReader reader = new XmlTextReader(s);   // Do something interesting with the XmlTextReader } static Stream GetDocumentStream(string address, ICredentials cred) {   XmlUrlResolver xur = new XmlUrlResolver();   Uri uri = new Uri(address);   if(cred != null)     xur.Credentials = cred;   try   {     return (Stream)xur.GetEntity(uri, null,null);   }   catch(Exception e)   {     MessageBox.Show(e.ToString());     throw e;   } } VB Function GetDocumentStream (ByVal address As String, ByVal cred As ICredentials) As Stream   Dim xur As New XmlUrlResolver   Dim uri As New Uri(address)   If (Not cred Is Nothing) Then     xur.Credentials = cred   End If   Try     Return xur.GetEntity(uri, Nothing, Nothing)   Catch ex As Exception     MessageBox.Show(ex.ToString())   End Try End Function Sub Main()   Dim url As String   Dim s As Stream   Dim cred As     New NetworkCredential("usrnm", "psswd", "domain")   url = "http://www.foo.com/bar.xml"   s = GetDocumentStream("url", cred)   Dim reader As New XmlTextReader(s)   ' Do something interesting with the XmlTextReader End Sub 

The most important section of this code is in the GetDocumentStream method. This method takes two parameters: the URL address of the XML resource as a string and an ICredentials object that represents the credentials with which to request this XML resource. To acquire the remote resource stream, we use the XmlURLResolver that inherits from the XmlResolver . We will discuss the XmlResolver later in this chapter, but for now think of the XmlResolver as an object that can grab resources from remote locations. We set the XmlURLResolver 's Credentials property to the specified ICredentials object. The XmlUrlResolver uses this ICredentials object when the GetEntity method needs to authenticate with a remote server. The "Entity" in GetEntity does not refer to an XML character entity but to anything that resides on a remote server. The GetEntity method returns a Stream object attached to the remote resource. This stream is returned to the caller of GetDocumentStream . Remember that even XML documents that were loaded from a remote location cannot contain a DOCTYPE node.

Back in the Main method, we create a NetworkCredentials object that is used to hold our authentication credentials. Then we call the GetDocumentStream method, passing in the URL to the resource and the newly created NetworkCredentials object. The GetDocumentStream returns the Stream to the remote XML resource; we pass this stream to the XmlTextReader constructor. This binds the XmlTextReader to the XML data stream. Subsequent read operations will pull data from the remote stream.

Retrieving a XML document from a Web Server by using SSL is trivial. Use the same code from Listing 10.3, but instead of specifying http://foo.com/bar.xml, we would pass in https ://foo.com/bar.xml if foo.com was using SSL. The XmlTextReader will handle the certificate authentication.



Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net