有没有什么方法可以保存XmlDocument *没有*缩进和换行符? [英] Is there any way to save an XmlDocument *without* indentation and line returns?
本文介绍了有没有什么方法可以保存XmlDocument *没有*缩进和换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我所有的搜索都引起了人们的反驳,但是如果保存为带有行返回和缩进的文件,我的文件将增长近50%.
All my searches have brought up people asking the opposite, but I have a file which grows by nearly 50% if it is saved with line returns and indentation.
有什么办法解决这个问题吗?
Is there any way round this?
编辑,我不是在谈论打开文件,而是保存一个文件.这段代码为我重现了"bug":
EDIT I'm not talking about opening a file, but saving one. This code reproduces the 'bug' for me:
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);
中间的断点表明doc.InnerXml实际上是< root>< line></line< line></line></root< code> ;,如预期的那样.但是最后
test.xml
的内容是:
A breakpoint in the middle shows that doc.InnerXml is effectively <root><line></line><line></line></root>
, as expected. But the contents of test.xml
at the end is:
<root>
<line>
</line>
<line>
</line>
</root>
推荐答案
尝试以下代码:
XmlDocument doc = new XmlDocument();
using(XmlTextWriter wr = new XmlTextWriter(fileName, Encoding.UTF8))
{
wr.Formatting = Formatting.None; // here's the trick !
doc.Save(wr);
}
这篇关于有没有什么方法可以保存XmlDocument *没有*缩进和换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文