开发者

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)
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜