tags:

views:

53

answers:

3

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

+4  A: 

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();
SLaks
Worked like a charm. Many thanks.
Josh Kodroff
+2  A: 

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);
simendsjo
A: 

You can use XMLBuilder to generate the XML and then call ToString method to get a indented output.

ararog