Converting RESTful XML output to formatted XML in Silverlight

Ever debugged output from a RESTful web service and was driven absolutely mad by the lack of formatting of the XML? Seriously, how much fun is it to stare at XML that has no line breaks or indentation whatsoever?

I haven’t tried to solve the problem within the IDE as that’s a different problem. However, I have written the code to convert the output into something that you could stuff into a TextBox for example (which is what I’ve done in my Silverlight application – I’ve created a small debug window for RESTFul web service calls).

 

HttpWebRequest request = (HttpWebRequest)
    HttpWebRequest.Create(new Uri("http://localhost:12345/MyWebService.asmx"));

request.BeginGetResponse(delegate(IAsyncResult ar)
{
    HttpWebRequest req = (HttpWebRequest)ar.AsyncState;
    HttpWebResponse webResponse = req.EndGetResponse(ar) as HttpWebResponse;
    try
    {
        using (Stream stream = webResponse.GetResponseStream())
        {
            XmlReader xmlReader = XmlReader.Create(stream);
            try
            {
                XmlWriterSettings settings = new XmlWriterSettings();                            
                settings.Indent = true;
                settings.Encoding = System.Text.Encoding.Unicode;
                using (StringWriter strWriter = new StringWriter())
                {
                    XmlWriter xmlWriter = XmlWriter.Create(strWriter, settings);
                    try
                    {
                        // cool, this recursively writes all of the nodes!
                        xmlWriter.WriteNode(xmlReader, false);

                        // output it to the UI as a string 
                        Dispatcher.BeginInvoke(delegate()
                        {
                            txtRawResults.Text = strWriter.ToString();
                        });
                    }
                    finally
                    {
                        xmlWriter.Close();
                    }
                }
            }
            finally
            {
                xmlReader.Close();
            }

            stream.Close();
        }
    }
    finally
    {
        webResponse.Close();
    }
}, request);

Replace the Uri in the first line with a call to your web service that returns XML.

Steps:

  1. The Response is requested asynchronously (as is the only option with Silverlight)
  2. Within the anonymous method, the request is retrieved from the IAyncResult object instance (ar)
  3. The async request is properly ended (EndGetResponse) and retreieved
  4. The stream is fetched from the response and passed to a new XmlReader instance.
  5. Create a new instance of the XmlWriterSettings object with indentation activated (Indent = True)
  6. Create a new XmlWriter instance using a new instance of a string writer and the settings created in step #5
  7. Recursively (thanks to the WriteNode method), write the entire document to the writer, formatted with indentations as requested!
  8. Invoke back to the UI thread with the result of the formatted, recursive WriteNode call.
  9. Clean up.