我有一个看起来有点像这样的XML文档:查找和数量的连续XML元素
<root>
Maybe some text
<thing>thing can have text</thing>
<thing>it can even be on multiple
lines
</thing>
<thing>a third thing</thing>
This text resets the numbering
<thing>this thing is not part of the above list and should have number 1</thing>
<some-element-not-thing>Also resets numbering</some-element-not-thing>
<thing>this thing should also have number 1<thing/>
</root>
我需要数点<thing>
时候,他们来连续,通过给每个被称为“数字”的属性。也就是说,我期望的结果是:
<root>
Maybe some text
<thing number="1">thing can have text</thing>
<thing number="2">it can even be on multiple
lines
</thing>
<thing number="3">a third thing</thing>
This text resets the numbering
<thing number="1">this thing is not part of the above list and should have number 1</thing>
<some-element-not-thing>Also resets numbering</some-element-not-thing>
<thing number="1">this thing should also have number 1<thing/>
</root>
我该如何处理这样的事情?我看不到在XmlDocument中的元素之间查找文本的方法(但它确实让我按顺序枚举元素,所以当遇到不是<thing>
的东西时,我可以重置编号),并且我不确定LINQ to XML是否允许我在元素之间获取文本,因为它只会产生元素或后代,而这两者都不代表“松散文本”。 也许这个“松散的文本”是坏的(但显然可以解析)XML?
编辑:我完全误解了我自己的问题。显然,元素之间没有文本,这是我之后修正的错误的结果。我最终使用的解决方案只是枚举节点并以这种方式改变它们的属性(使用XML文档并忽略空白),类似于下面的建议。我很抱歉没有把这个问题转到我的头上,或者花费更多的时间进行研究。如果人们认为这个问题没有足够的贡献,我不会介意删除它。
为什么要使用'XmlDocument'任何理由吗?除非我有一个非常好的理由,否则我总是使用LINQ to XML。 –
这看起来好像不是全部有效的XML。 –
请不要只是要求我们为您解决问题。告诉我们你是如何试图自己解决问题的,然后向我们展示结果是什么,并告诉我们为什么你觉得它不起作用。请参阅“[您尝试过什么?](http://whathaveyoutried.com/)”,以获得一篇您最近需要阅读的优秀文章。 –