whihathac's Den

code is poetry

Format XML in C#

Found an interesting link - http://www.shrinkrays.net/code-snippets/csharp/format-xml-in-csharp.aspx

This is to format XML in C#. I have found it quite useful to view and analyze the XML returned from the Web Services.

/// <summary> 
/// Formats the provided XML so it's indented and humanly-readable. 
/// </summary> 
/// <param name="inputXml">The input XML to format.</param> 
/// <returns></returns> 
public static string FormatXml(string inputXml) 
    XmlDocument document = new XmlDocument(); 
    document.Load(new StringReader(inputXml)); 
    StringBuilder builder = new StringBuilder(); 
    using (XmlTextWriter writer = new XmlTextWriter(new StringWriter(builder))) 
        writer.Formatting = Formatting.Indented; 
    return builder.ToString();