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