C# 基于xpath谓词的组节点

C# 基于xpath谓词的组节点,c#,xpath,C#,Xpath,我有类似xpath的字符串 /root/parent/child /root/parent/child[1] /root/parent/child[2] 在c#代码中,我检查XmlDocument中是否存在xpath,并像这样克隆它 //Get the parent node of the node to be cloned XmlNode NodeTobeCloned = xmlDoc.SelectSingleNode(oRow[0].ToString()); XmlNode Duplica

我有类似xpath的字符串

/root/parent/child
/root/parent/child[1]
/root/parent/child[2]
在c#代码中,我检查XmlDocument中是否存在xpath,并像这样克隆它

//Get the parent node of the node to be cloned
XmlNode NodeTobeCloned = xmlDoc.SelectSingleNode(oRow[0].ToString());
XmlNode DuplicateNode = NodeTobeCloned.CloneNode(true);
DuplicateNode.InnerText = sValue;
//Insert the node after the last child of a commoon parent
NodeTobeCloned.ParentNode.AppendChild(DuplicateNode);
我得到的结果是

<root>
<parent>
<child/>
<child/>
<child/>
</parent>
</root>
<root>
<parent>
<child/>
</parent>
<parent>
<child/> -- [1] -predicates elements
</parent>
<parent>
<child/> -- [2] -predicates elements
</parent>
</root>

我想要一个这样的结果

<root>
<parent>
<child/>
<child/>
<child/>
</parent>
</root>
<root>
<parent>
<child/>
</parent>
<parent>
<child/> -- [1] -predicates elements
</parent>
<parent>
<child/> -- [2] -predicates elements
</parent>
</root>

--[1]-谓词元素
--[2]-谓词元素
如何使用c向xmldocument追加内容#
谢谢

/root/parent/child和/root/parent/child[1]指的是同一个节点。你是说[2]和[3]吗?原始XML看起来像什么?你是说你想创建额外的
节点来放置
ren吗?是的,假设谓词从0开始是我的错误。你是对的,它从1开始是的,我想创建额外的父节点来放置子节点。实际上,我有一个逻辑组存储为xpath字符串,如/root/parent/child/root/parent/child[2]/root/parent/anotherchild[2]我想根据谓词对任何指向问题的指针进行分组?我认为这不是一个很好的设计,我认为问题的参数没有很好的定义。你手头的实际任务是什么?为什么要使用XPath指定要创建的节点?XPath几乎总是用于查询XML,而不是添加到XML中。我很难理解您的问题,您能否发布原始XML以及“oRow[0].ToString()”的含义