如何保存的XmlDocument多个缩进设置? [英] How to save XmlDocument with multiple indentation settings?

查看:221
本文介绍了如何保存的XmlDocument多个缩进设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要救一个的XmlDocument 用正确的缩进到文件(Formatting.Indented)但也有一些节点的孩子们必须在同一行(Formatting.None)

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).

如何做到这一点,因为的XmlTextWriter 接受设置为整个文档?

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

@Ahmad Mageed的resposne后编辑:

Edit after @Ahmad Mageed's resposne:

我不知道XmlTextWriter的设置可以在书面修改。这是个好消息。

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

现在我节省的XmlDocument(这已经是充满了节点,要具体是的.xaml页)这种方式:

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();

它能使当然缩进的所有节点。我需要禁用缩进当处理所有<润GT; 节点

在你的榜样,你写的XmlTextWriter手动。 有没有一种简单的方法来抓取通过所有的XmlDocument节点,并将其写入XmlTextWriter的,所以我可以检测<运行> 节点?或者,我必须写某种递归的方法,将进入当前节点的每一个孩子?

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?

推荐答案

你是什么意思,因为XmlTextWriter的接受设置为整个文档?所述的XmlTextWriter的设置可以修改的,不像的XmlWriter的一次设置。同样的,你如何使用XmlDocument的?请张贴一些code,以表明你已经尝试过让其他人有更好的理解这个问题。

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.

如果我理解正确的,您可以修改的XmlTextWriter的格式会影响你希望出现在一行中的节点。一旦你做了,你会重新格式化回缩进。

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.

例如,这样的事情:

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();

这篇关于如何保存的XmlDocument多个缩进设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆