如何找到一个特定的XML节点?节点、XML

2023-09-05 23:09:51 作者:只在你伤心时,我才伤心

我有这个问题找到一个特定的XML节点l具备发布此问题上的计算器和一些不错的研究员建议的XPath。

i have this problem to find a particular xml node l have post this problem on stackoverflow and some nice fellows suggested xpath.

我是一个xml新手。请我需要一个C#code寻父,父,父为(大盛大父),那么第一个孩子,lastchild,lastchild。在code必须遍历了树,并再次回落。将在大量的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树。长期和短期的这一切是我需要查找节点基础上,它的位置离当前节点

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

我的意思寻找currentnode的父父父(伟大的宏伟parentnode),然后找到的第一个孩子,然后找到lastchild lastchild

i meant finding the parent parent parent of a currentnode (great grand parentnode )then find the first child then find the lastchild lastchild

currentnode.parentnode.parentnode.parentnode.firstchild.lastchild.lastchild的keepkind;

keepkind of currentnode.parentnode.parentnode.parentnode.firstchild.lastchild.lastchild;

使用XPath C#

推荐答案

假设你有一个名为XMLNode实例节点开始。那么下面code会给你该节点的伟大盛大父母的第一个孩子的最后一个孩子的最后一个子:

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 wantedNode = node.ParentNode.ParentNode.ParentNode.FirstChild.LastChild.LastChild;

请注意,有这么多的东西可以去错了这个code。如果有任何引用的节点正好是空,你有一个NullReferenceException到来。所以,你会希望做一个空检查在每个级别:

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:

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;
}

让我们来看看这有一个更具体的例子。假设我们有下面的XML文档:

Let's examine this with a more concrete example. Assume we have the following Xml document:

<?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>

如果我理解你的问题吧,如果我们从节点开始&LT; /儿童&GT;孩子someValue中=3&GT;&LT 我们要导航到&LT;孩子someValue中=5&GT;&LT; /儿童&GT; 。上面的code样品将这样做。然而,如所提到的,很容易出现给予异常如果不是所有预期节点都是present

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.

虽然你说你想要C#code,而不是XPath的,在这种情况下,我觉得XPath是要走的路。有许多方法来解决这个问题。例如,如果节点有不同的标记名称(如我的示例文件),而是可以这样做:

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:

XmlNode wantedNode = node.SelectSingleNode("ancestor::greatgrandparent/grandparent[position()=1]/parent[position()=last()]/child[position()=last()]");
if (wantedNode != null)
{
    // the path was found
}

这当然是假设节点不是空的,但有效的XmlNode实例。

This is of course assuming that node is not null, but a valid XmlNode instance.

的XPath的前pression分析:

Breakdown of the XPath expression:

在祖先:: greatgrandparent - >这将找到名为greatgrandparent位于任何地方向上在层次结构(所以任何父母,祖父母等)的任何节点 /祖父母[位置()= 1] - >命名的第一个子节点祖父母 /亲本的位置()=上()] - >命名的最后一个子节点父 /子[位置()=上()] - >命名的最后一个子节点子

如果你想读一些关于如何XPath的轴工作,有在 w3schools.com 。

If you want to read some about how XPath axes work, there is some information on w3schools.com.