[英]XPath: How to select the first given parent of a node?
My XML: 我的XML:
<root>
<child>
<childOfChild>
<anotherLostChild>
<currentSelectedNode>
SOME TEXT
</currentSelectedNode>
</anotherLostChild>
</childOfChild>
</child>
</root>
I selected the node currentSelectedNode using: 我使用以下方法选择了节点currentSelectedNode :
xpath.SelectSingleNode("//currentSelectedNode")
But How can I back to select the first chilfOfChild parent node (considering that the context is currentSelectedNode ? 但是,如何返回选择第一个chilfOfChild父节点(考虑到上下文是currentSelectedNode ?
xpath.SelectSingleNode("//currentSelectedNode")...???
Your question is really confusingly written but it sounds like you'd want the ancestor axis , something like: 你的问题真的很混乱,但听起来你想要祖先的轴 ,比如:
//currentSelectedNode/ancestor::childOfChild[1]
(pure xpath solution) (纯xpath解决方案)
xpath.SelectSingleNode("//currentSelectedNode/../..")
这将选择父母的父母
In Linq-To-XML you would need only to use the Ancestor method: 在Linq-To-XML中,您只需要使用Ancestor方法:
To get the immediate ancestor: 要获得直接的祖先:
xElement.Ancestor();
To specify which ancestor: 要指定哪个祖先:
xElement.Ancestor("NameOfTheAncestorNode");
Using XElement xElem
, you can use xElem.Parent
to get the immediate parent. 使用XElement xElem
,您可以使用xElem.Parent
来获取直接父级。
https://msdn.microsoft.com/en-us/library/system.xml.linq.xobject.parent(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.xml.linq.xobject.parent(v=vs.110).aspx
To select specific ancestor nodes, you can use 要选择特定的祖先节点,您可以使用
xElem.Ancestors("currentSelectedNode");
to get all ancestors with that specific node name. 获取具有该特定节点名称的所有祖先。
https://msdn.microsoft.com/en-us/library/bb348268(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/bb348268(v=vs.110).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.