如何保存具有多个缩进设置的XmlDocument?

发布于 2024-08-17 11:55:35 字数 747 浏览 2 评论 0原文

我需要将一个 XmlDocument 保存到具有正确缩进 (Formatting.Indented) 的文件中,但某些节点及其子节点必须位于一行 (Formatting.None)

既然 XmlTextWriter 接受整个文档的设置,如何实现这一点?


在 @Ahmad Mageed 的回复后编辑:

我不知道 XmlTextWriter 设置可以在写入过程中修改。这是个好消息。

现在,我正在以这种方式保存 xmlDocument(它已经充满了节点,具体来说是 .xaml 页面):

XmlTextWriter writer = new XmlTextWriter(filePath, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
xmlDocument.WriteTo(writer);
writer.Flush();
writer.Close();

当然,它可以在所有节点中启用缩进。我需要在处理所有 节点时禁用缩进。

在您的示例中,您“手动”写入 XmlTextWriter。 有没有一种简单的方法可以爬取所有 xmlDocument 节点并将它们写入 XmlTextWriter,以便我可以检测 节点?或者我是否必须编写某种递归方法来访问当前节点的每个子节点?

I need to save one XmlDocument to file with proper indentation (Formatting.Indented) but some nodes with their children have to be in one line (Formatting.None).

How to achieve that since XmlTextWriter accept setting for a whole document?


Edit after @Ahmad Mageed's resposne:

I didn't know that XmlTextWriter settings can be modified during writing. That's good news.

Right now I'm saving xmlDocument (which is already filled with nodes, to be specific it is .xaml page) this way:

XmlTextWriter writer = new XmlTextWriter(filePath, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
xmlDocument.WriteTo(writer);
writer.Flush();
writer.Close();

It enables indentation in all nodes, of course. I need to disable indentation when dealing with all <Run> nodes.

In your example you write to XmlTextWriter "manually".
Is there an easy way to crawl through all xmlDocument nodes and write them to XmlTextWriter so I can detect <Run> nodes? Or do I have to write some kind of recursive method that will go to every child of current node?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

瞄了个咪的 2024-08-24 11:55:36

“因为 XmlTextWriter 接受整个文档的设置”是什么意思? XmlTextWriter 的设置可以修改,这与 XmlWriter 的一次设置不同。同样,您如何使用 XmlDocument?请发布一些代码来展示您所尝试的内容,以便其他人更好地理解该问题。

如果我理解正确,您可以修改 XmlTextWriter 的格式以影响要显示在一行上的节点。完成后,您可以将格式重置为缩进。

例如,这样的事情:

XmlTextWriter writer = new XmlTextWriter(...);
writer.Formatting = Formatting.Indented;
writer.Indentation = 1;
writer.IndentChar = '\t';

writer.WriteStartElement("root");

// people is some collection for the sake of an example
for (int index = 0; index < people.Count; index++)
{
    writer.WriteStartElement("Person");

    // some node condition to turn off formatting
    if (index == 1 || index == 3)
    {
        writer.Formatting = Formatting.None;
    }

    // write out the node and its elements etc.
    writer.WriteAttributeString("...", people[index].SomeProperty);
    writer.WriteElementString("FirstName", people[index].FirstName);

    writer.WriteEndElement();

    // reset formatting to indented
    writer.Formatting = Formatting.Indented;
}

writer.WriteEndElement();
writer.Flush();

What do you mean by "since XmlTextWriter accept setting for a whole document?" The XmlTextWriter's settings can be modified, unlike XmlWriter's once set. Similarly, how are you using XmlDocument? Please post some code to show what you've tried so that others have a better understanding of the issue.

If I understood correctly, you could modify the XmlTextWriter's formatting to affect the nodes you want to appear on one line. Once you're done you would reset the formatting back to be indented.

For example, something like this:

XmlTextWriter writer = new XmlTextWriter(...);
writer.Formatting = Formatting.Indented;
writer.Indentation = 1;
writer.IndentChar = '\t';

writer.WriteStartElement("root");

// people is some collection for the sake of an example
for (int index = 0; index < people.Count; index++)
{
    writer.WriteStartElement("Person");

    // some node condition to turn off formatting
    if (index == 1 || index == 3)
    {
        writer.Formatting = Formatting.None;
    }

    // write out the node and its elements etc.
    writer.WriteAttributeString("...", people[index].SomeProperty);
    writer.WriteElementString("FirstName", people[index].FirstName);

    writer.WriteEndElement();

    // reset formatting to indented
    writer.Formatting = Formatting.Indented;
}

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