Skip to content

Instantly share code, notes, and snippets.

@yetanotherchris
Created February 11, 2013 20:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yetanotherchris/4757231 to your computer and use it in GitHub Desktop.
Save yetanotherchris/4757231 to your computer and use it in GitHub Desktop.
XML formatter for C#
/// <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;
document.Save(writer);
}
return builder.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment