默认输出的 InnerXml 显示在一行,看起来非常痛苦,特别是复杂XML文档,输出格式化后的 InnerXml 可以方便快速的查看。
public string GetFormattedInnerXml(XmlDocument doc)
{
StringBuilder builder = new StringBuilder();
using (StringWriter sw = new StringWriter(builder))
{
using (XmlTextWriter xtw = new XmlTextWriter(sw))
{
xtw.Formatting = Formatting.Indented;
xtw.Indentation = 1;
xtw.IndentChar = '\t';
doc.WriteTo(xtw);
}
}
return builder.ToString();
}