When I build XML up from scratch with XmlDocument
, the OuterXml
property already has everything nicely indented with line breaks. However, if I call LoadXml
on some very "compressed" XML (no line breaks or indention) then the output of OuterXml
stays that way. So ...
What is the simplest way to get beautified XML output from an instance of XmlDocument
?
Or even easier if you have access to Linq
As adapted from Erika Ehrli's blog, this should do it:
A simple way is to use:
Like this sample code, this code is what I used to create a tree view like structure using XMLWriter :
This way you can add tab or line breaks in the way you are normally used to, i.e. \t or \n
When implementing the suggestions posted here, I had trouble with the text encoding. It seems the encoding of the
XmlWriterSettings
is ignored, and always overridden by the encoding of the stream. When using aStringBuilder
, this is always the text encoding used internally in C#, namely UTF-16.So here's a version which supports other encodings as well.
IMPORTANT NOTE: The formatting is completely ignored if your
XMLDocument
object has itspreserveWhitespace
property enabled when loading the document. This had me stumped for a while, so make sure not to enable that.My final code:
This will save the formatted xml to disk, with the given text encoding.
If the above Beautify method is being called for an
XmlDocument
that already contains anXmlProcessingInstruction
child node the following exception is thrown:This is my modified version of the original one to get rid of the exception:
It works for me now, probably you would need to scan all child nodes for the
XmlProcessingInstruction
node, not just the first one?Update April 2015:
Since I had another case where the encoding was wrong, I searched for how to enforce UTF-8 without BOM. I found this blog post and created a function based on it: