有没有办法保存XmlDocument*没有*缩进和行返回?

Ben*_*jol 12 .net c# xmldocument

我所有的搜索都让人们提出了相反的问题,但如果用行返回和缩进保存,我有一个文件增长了近50%.

这有什么办法吗?

编辑我不是在谈论打开文件,而是保存文件.此代码为我重现了'bug':

var path = @"C:\test.xml";
System.IO.File.WriteAllText(path, "<root>\r\n\t<line></line>\r\n\t<line></line>\r\n</root>");
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.PreserveWhitespace = false;
doc.Load(path);
doc.PreserveWhitespace = false; //just in case!
doc.Save(path);
Run Code Online (Sandbox Code Playgroud)

中间的断点显示doc.InnerXml是有效的<root><line></line><line></line></root>,正如预期的那样.但最后的内容test.xml是:

<root>
  <line>
  </line>
  <line>
  </line>
</root>
Run Code Online (Sandbox Code Playgroud)

dig*_*All 24

试试这段代码:

XmlDocument doc = new XmlDocument();

using(XmlTextWriter wr = new XmlTextWriter(fileName, Encoding.UTF8))
{
    wr.Formatting = Formatting.None; // here's the trick !
    doc.Save(wr);
}
Run Code Online (Sandbox Code Playgroud)