Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET XML Pretty Printer?

Tags:

.net

xml

Is there a method in the .NET Framework or a free Open Source library to pretty print XML?

like image 930
Josh Kodroff Avatar asked Jun 17 '10 15:06

Josh Kodroff


2 Answers

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();
like image 163
SLaks Avatar answered Oct 13 '22 10:10

SLaks


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);
like image 41
simendsjo Avatar answered Oct 13 '22 09:10

simendsjo