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
?
A more simplified approach based on the accepted answer:
Setting the new line is not necessary. Indent characters also has the default two spaces so I preferred not to set it as well.
If you have a string of XML, rather than a doc ready for use, you can do it this way:
Based on the other answers, I looked into
XmlTextWriter
and came up with the following helper method:It's a bit more code than I hoped for, but it works just peachy.
A shorter extension method version