XmlWriterSettings、XmlTextWriter、XmlWriter 未格式化输出?!没有新行,没有缩进
在 VS2008 中使用 .Net 3.5 SP1 我有一个 XmlDocument 并尝试将其写入文件:
using (XmlTextWriter tw = new XmlTextWriter(outXmlFileName, System.Text.Encoding.UTF8))
{
tw.Formatting = Formatting.Indented;
tw.Indentation = 3;
tw.IndentChar = ' ';
tw.QuoteChar = '\'';
doc.Save(tw);
}
。
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(outXmlFileName, settings))
{
doc.Save(writer);
}
文档中不清楚的内容,http://msdn.microsoft.com/en-us/library/kkz7cs0d.aspx,如果我应该使用 .Net 2 上面的 XmlWriter.Create() ,无论如何,两种方法都不会格式化输出!我刚刚得到 XmlDocument 实例中的内容:
<?xml version='1.0' encoding='utf-8'?>
<root>
<node1 />
<node2 value='Data' />
<node3 value='ID' /><node4><item>
<from value='1 Jan 1870' />
<id value='PF' />
<to value='1 Jan 1940' /></item></node4>
</root>
How can I tidy my XML before write it file from .Net?!
Using .Net 3.5 SP1 in VS2008 I have a XmlDocument and have tried writing it to file:
using (XmlTextWriter tw = new XmlTextWriter(outXmlFileName, System.Text.Encoding.UTF8))
{
tw.Formatting = Formatting.Indented;
tw.Indentation = 3;
tw.IndentChar = ' ';
tw.QuoteChar = '\'';
doc.Save(tw);
}
And
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(outXmlFileName, settings))
{
doc.Save(writer);
}
What is unclear from the documentation, http://msdn.microsoft.com/en-us/library/kkz7cs0d.aspx, is if I should be using XmlWriter.Create() above .Net 2, anyway either way neither methods format the output! I just get what is in the XmlDocument instance:
<?xml version='1.0' encoding='utf-8'?>
<root>
<node1 />
<node2 value='Data' />
<node3 value='ID' /><node4><item>
<from value='1 Jan 1870' />
<id value='PF' />
<to value='1 Jan 1940' /></item></node4>
</root>
How can I tidy my XML before writing it file from .Net?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过将 XML 加载到第二个中间 XmlDocument 实例中来使其工作,因此它忘记了原始的空白:
I got it to work by loading the XML into an 2nd intermediary XmlDocument instance so it forgot the original white space: