Format an XmlDocument as an indented string

This is an easy way of formatting an XmlDocument into an indented string. Could be useful for logging, etc?

XmlDocument doc = ...;
...
StringWriter sw = new StringWriter();
XmlTextWriter xtw = new XmlTextWriter(sw);
xtw.Formatting = Formatting.Indented;
doc.WriteTo(xtw);
Console.WriteLine(sw.ToString());

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.