-
-
Save LSTANCZYK/a0257b573317bac7bb5e96f1c7e25900 to your computer and use it in GitHub Desktop.
Example of pretty-printing XML in C# using the XmlWriter class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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