Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Created December 4, 2011 18:52
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kristopherjohnson/1430976 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/1430976 to your computer and use it in GitHub Desktop.
Example of pretty-printing XML in C# using the XmlWriter class
using System;
using System.Text;
using System.Xml;
using System.Xml.Linq;
static string PrettyXml(string xml)
{
var stringBuilder = new StringBuilder();
var element = XElement.Parse(xml);
var settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
settings.NewLineOnAttributes = true;
using (var xmlWriter = XmlWriter.Create(stringBuilder, settings))
{
element.Save(xmlWriter);
}
return stringBuilder.ToString();
}
@richardalgor
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment