XmlWriterSettings、XmlTextWriter、XmlWriter 未格式化输出?!没有新行,没有缩进

发布于 2024-12-06 05:28:37 字数 1138 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仄言 2024-12-13 05:28:37

我通过将 XML 加载到第二个中间 XmlDocument 实例中来使其工作,因此它忘记了原始的空白:

            XmlWriterSettings settings = new XmlWriterSettings(); // http://msdn.microsoft.com/en-us/library/kkz7cs0d(VS.85).aspx
            settings.Indent = true;

            XmlDocument doc2 = new XmlDocument();
            doc2.LoadXml(doc.OuterXml);

            using (XmlWriter writer = XmlWriter.Create(outXmlFileName, settings))
            {
                doc2.Save(writer);
            }

I got it to work by loading the XML into an 2nd intermediary XmlDocument instance so it forgot the original white space:

            XmlWriterSettings settings = new XmlWriterSettings(); // http://msdn.microsoft.com/en-us/library/kkz7cs0d(VS.85).aspx
            settings.Indent = true;

            XmlDocument doc2 = new XmlDocument();
            doc2.LoadXml(doc.OuterXml);

            using (XmlWriter writer = XmlWriter.Create(outXmlFileName, settings))
            {
                doc2.Save(writer);
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文