Skip to content

Instantly share code, notes, and snippets.

@LSTANCZYK
Forked from kristopherjohnson/PrettyXml.cs
Created November 27, 2017 20:22
Show Gist options
  • Save LSTANCZYK/a0257b573317bac7bb5e96f1c7e25900 to your computer and use it in GitHub Desktop.
Save LSTANCZYK/a0257b573317bac7bb5e96f1c7e25900 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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment