How to tidy a .Net XmlDocument text before writing it to file?
Using XmlDocument Class: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument(VS.85).as开发者_开发百科px
How can I tidy the XML text so it is indented and placed on new lines correctly before calling Save()?
Use the Save() overload that takes an XmlWriter and configure the XmlWriter using a XmlWriterSettings instance. For example
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = " ";
settings.NewLineOnAttributes = true;
//Using XmlWriter to create xml file.
using (XmlWriter writer = XmlWriter.Create("some-file.xml",settings))
{
doc.Save(writer)
}
精彩评论