如何在将.Net XmlDocument文本写入文件之前整理它?

时间:2011-08-11 10:36:51

标签: .net c#-3.0 xmldocument

使用XmlDocument类:http://msdn.microsoft.com/en-us/library/system.xml.xmldocument(VS.85).aspx

如何在调用Save()之前整理XML文本以使其缩进并正确放置在新行上?

1 个答案:

答案 0 :(得分:1)

使用带有XmlWriter的Save()重载并使用XmlWriterSettings实例配置XmlWriter。例如

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