XPathNavigator.MoveToFollowing和EOF [英] XPathNavigator.MoveToFollowing and EOF

查看:108
本文介绍了XPathNavigator.MoveToFollowing和EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我目前有从xml文件读取并在另一个文件中输出的代码

Hello everybody,
I currently have this code that reads from a xml file and outputs it in an another file

                XmlReader xml_input = XmlReader.Create("testinput.xml");
                XmlWriter xml_output = XmlWriter.Create(File.Create("testoutput.xml"));
               
                XPathDocument xd = new XPathDocument(xml_input);
                XPathNavigator xn = xd.CreateNavigator();

while (xn.MoveToFollowing(XPathNodeType.Element))
{
    xml_output.WriteNode(xn, true);
}


我遇到的问题是,一旦到达最后一个结束标签,它就会回到第一个子元素,然后继续下去,就像这样:

输入:


the problem I''ve got is that once the last end tag is reached, it goes back to the first child element and so go on, like this:

input:

<?xml version="1.0" encoding="utf-16"?>
<walalala>
    <abcde att="asd">
        <ffffff att="1231" />
    </abcde>
</walalala>



输出:



output:

<walalala>
    <abcde att="asd">
        <ffffff att="1231" />
    </abcde>
</walalala>
<abcde att="asd">
    <ffffff att="1231" />
</abcde>
<ffffff att="1231" />


我希望它在到达终点时停止.
我该怎么办?


I would like it to stop when it reaches the end.
How can I do it?

推荐答案

对自己的回答:WriteNode实际上在XPathNavigator中写入了所有节点及其子元素,所以这就是为什么.我想没有一种便宜的方法可以只复制一个元素的行,所以我想我会用它的属性重建每个元素.
Answering to myself: WriteNode actually writes all the node and its child elements in the XPathNavigator, so this is why that''s happening. I guess there''s no cheap way to copy just an element''s line, so I think I''ll rebuild each element with its attributes.


这篇关于XPathNavigator.MoveToFollowing和EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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