Is there a method in the .NET Framework or a free Open Source library to pretty print XML?
All of .Net's standard XML APIs will format their output.
Using LINQ to XML:
string formatted = XDocument.Parse(source).ToString();
Or
string formatted = XDocument.Load(path).ToString();
Use the XmlWriterSettings with an XmlWriter
var doc = new XmlDocument();
doc.Load(@"c:\temp\asdf.xml");
var writerSettings = new XmlWriterSettings
{
Indent = true,
NewLineOnAttributes = true,
};
var writer = XmlWriter.Create(@"c:\temp\asdf_pretty.xml", writerSettings);
doc.Save(writer);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With