Loading an XML file from a stream

I’ve been working with XML files being returned from the Flickr web service. The authentication scheme for Flickr using their APIs is a little involved, and I’ll get more into that process in a future blog post.

One step in the authentication process is returning a “frob” for a Flickr application. The results of the request are in XML format (unless you choose differently) and look like this:

<rsp stat="ok">
  <frob>72157625513700330-d4435917b66c4c17-220944</frob>
</rsp>

I was doing this asynchronously, and the results were returned as a stream. Here is my finished function, which converts the stream (the parameter e.Result is a stream) into a string and uses XmlDocument.LoadXml to load the XML:

            XmlDocument doc = new XmlDocument();
            StreamReader Reader = new StreamReader(e.Result);
            doc.LoadXml(Reader.ReadToEnd());
            XmlNodeList Node = doc.GetElementsByTagName("frob");
            Response.Write(Node[0].FirstChild.Value);

Afterwards, a simple called to the XmlNodeList.GetElementsByTagName to retrieve the correct tag, and a call to the FirstChild property of the XML node, was all that was needed.

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: