开发者

How to tidy a .Net XmlDocument text before writing it to file?

开发者 https://www.devze.com 2023-03-27 01:53 出处:网络
Using XmlDocument Class: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument(VS.85).as开发者_开发百科px

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

精彩评论

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