C# 如何找到特定的xml节点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/886906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find a specific xml node?
提问by Spikie
i have this problem to find a particular xml node l have post this problem on stackoverflow and some nice fellows suggested xpath.
我有这个问题要找到一个特定的 xml 节点我已经在 stackoverflow 上发布了这个问题,一些好人建议使用 xpath。
I am an xml newbie . please I need an c# code to find the parent , parent, parent as (great grand parent) then the first child ,lastchild , lastchild . the code have to iterate up the tree and down again. Be looking at a lot of xpath tutorials online.
我是 xml 新手。请我需要一个 C# 代码来找到父级、父级、父级(曾祖父母),然后是第一个孩子、最后一个孩子、最后一个孩子。代码必须向上和向下迭代树。在线查看大量 xpath 教程。
I discovered that the path tend to be specific to a particular node already existing . The program that I need will not get no particular named node because at each pass a new node will be add to the xml tree. The long and short of it all is that I need find the node base on it's position away from the current node
我发现路径往往特定于已经存在的特定节点。我需要的程序不会得到任何特定的命名节点,因为在每次传递时,都会将一个新节点添加到 xml 树中。总而言之,我需要根据它远离当前节点的位置找到节点
i meant finding the parent parent parent of a currentnode (great grand parentnode )then find the first child then find the lastchild lastchild
我的意思是找到当前节点的父父节点(伟大的大父节点)然后找到第一个子节点然后找到最后一个子节点
keepkind of currentnode.parentnode.parentnode.parentnode.firstchild.lastchild.lastchild;
保持currentnode.parentnode.parentnode.parentnode.firstchild.lastchild.lastchild的种类;
using xpath C#
使用 xpath C#
采纳答案by Fredrik M?rk
Let's say that you have an XmlNode instance called node
to start with. Then the following code will give you the last child of the last child of the first child of the great grand parent of that node:
假设您有一个被调用的 XmlNode 实例node
。然后以下代码将为您提供该节点的曾祖父母的第一个孩子的最后一个孩子的最后一个孩子:
XmlNode wantedNode = node.ParentNode.ParentNode.ParentNode.FirstChild.LastChild.LastChild;
Note that there are so many things that can go wrong with this code. If any of the referenced nodes happen to be null, you have a NullReferenceException coming. So you will want to make a null check at each level:
请注意,此代码可能会出错的地方太多了。如果任何引用的节点碰巧为空,则会出现 NullReferenceException。因此,您需要在每个级别进行空检查:
XmlNode wantedNode;
if (node.ParentNode != null && node.ParentNode.ParentNode != null /* and so on through the full path */)
{
wantedNode = node.ParentNode.ParentNode.ParentNode.FirstChild.LastChild.LastChild;
}
Let's examine this with a more concrete example. Assume we have the following Xml document:
让我们用一个更具体的例子来检验这一点。假设我们有以下 Xml 文档:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<greatgrandparent>
<grandparent>
<parent id="1">
<child somevalue="3"></child>
</parent>
<parent id="2">
<child somevalue="4"></child>
<child somevalue="5"></child>
</parent>
</grandparent>
</greatgrandparent>
</root>
If I understand your question right, if we start from the node <child somevalue="3"></child>
we want to navigate to <child somevalue="5"></child>
. The code sample above will do that. However, as mentioned, it is prone to giving exceptions if not all expected nodes are present.
如果我正确理解您的问题,如果我们从<child somevalue="3"></child>
要导航到的节点开始<child somevalue="5"></child>
。上面的代码示例将做到这一点。但是,如前所述,如果不是所有预期节点都存在,则很容易出现异常。
Even though you said that you want c# code rather than XPath, in this case I feel that XPath is the way to go. There are a number of ways to solve this. For instance, if the nodes have different tag names (as in my sample document), you can instead do like this:
即使你说你想要 c# 代码而不是 XPath,在这种情况下我觉得 XPath 是要走的路。有很多方法可以解决这个问题。例如,如果节点具有不同的标签名称(如我的示例文档中所示),您可以这样做:
XmlNode wantedNode = node.SelectSingleNode("ancestor::greatgrandparent/grandparent[position()=1]/parent[position()=last()]/child[position()=last()]");
if (wantedNode != null)
{
// the path was found
}
This is of course assuming that node
is not null, but a valid XmlNode instance.
这当然是假设它node
不为空,而是一个有效的 XmlNode 实例。
Breakdown of the XPath expression:
XPath 表达式的分解:
- ancestor::greatgrandparent -> this will locate any node named "greatgrandparent" that is located anywhere upwards in the hierarchy (so any parent, grand parent and so on)
- /grandparent[position()=1] -> the first child node named "grandparent"
- /parent[position()=last()] -> the last child node named "parent"
- /child[position()=last()] -> the last child node named "child"
- ancestor::greatgrandparent -> 这将定位位于层次结构中任何位置的任何名为“greatgrandparent”的节点(因此任何父节点、祖父节点等)
- /grandparent[position()=1] -> 第一个名为“grandparent”的子节点
- /parent[position()=last()] -> 最后一个名为“parent”的子节点
- /child[position()=last()] -> 最后一个名为“child”的子节点
If you want to read some about how XPath axes work, there is some information on w3schools.com.
如果您想阅读一些有关 XPath 轴如何工作的信息,可以在w3schools.com上找到一些信息。
回答by samjudson
All the question you are asking are answered by the XmlNodeclass. It has properties called ParentNode
, FirstChild
and LastChild
which each return another XmlNode
.
您提出的所有问题都由XmlNode类回答。它具有的属性称为ParentNode
,FirstChild
并且LastChild
每个返回另一个XmlNode
。
To do the same thing in XPath you can use the ".."
abbreviation to get a nodes parent, and "*[position()=1]"
or "*[position()=last()]"
to get the first and last child, e.g.:
要在 XPath 中做同样的事情,您可以使用".."
缩写来获取节点的父节点,"*[position()=1]"
或者"*[position()=last()]"
获取第一个和最后一个子节点,例如:
XmlNode foundNode = node.SelectSingleNode("../../../*[position()=1]/*[position()=last()]/*[position()=last()]");
(Notes: ".." is an abbreviation of the parent::* axis, and "*" is an abbreviation of the "child::*" axis)
(注:“..”是parent::*轴的缩写,“*”是“child::*”轴的缩写)