如何获取xml节点的兄弟节点 [英] How to get the sibling of an xml node

查看:41
本文介绍了如何获取xml节点的兄弟节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的xml文件

I have an xml file as below

<Games>
   <Game>
      <name>Tzoker</name>
      <file>tzoker1</file>
   </Game>
   <Game>
      <file>lotto770</file>
   </Game>
   <Game>
      <name>Proto</name>
      <file>proto220</file>
   </Game>
</Games>

我想获取每个游戏节点的名称和文件项的值.使用此查询很容易.

I want to get the values of name and file items for every Game node. It is easy by using this query.

string query = String.Format("//Games/Game");
 XmlNodeList elements1 = xml.SelectNodes(query);
 foreach (XmlNode xn in elements1)
 {
   s1 = xn["name"].InnerText;
   s2 = xn["file"].InnerText;
 }

问题是有些节点没有名称项.所以上面的代码不起作用.我已经通过使用以下代码解决了这个问题

The problem is that there are some nodes that they don't have the name item. So the code above doesn't work. I have solved the problem by using the following code

string query = String.Format("//Games/Game/name");
XmlNodeList elements1 = xml.SelectNodes(query);
foreach (XmlNode xn in elements1)
{
  s1 = xn.InnerText;
  string query1 = String.Format("//Games/Game[name='{0}']/file", s1);
  XmlNodeList elements2 = xml.SelectNodes(query1);
  foreach (XmlNode xn2 in elements2)
  {
    s2 = xn2.InnerText;
  }
}

问题是存在两个或多个节点具有相同名称值的情况.因此, s2 变量将获取循环找到的最后一个节点的文件值.所以,我想找到一种方法来获取当前名称项的兄弟文件值.我怎么能做到?我尝试使用以下代码移动到当前节点的父节点,然后移动到文件项但没有成功.

The problem is that there is a case that two or more nodes have the same name value. So, the s2 variable will get the file value of the last node that the loop finds. So, I would like to find a way to get the sibling file value of the current name item. How could I do it? I try do move to the parent node of the current node and then to move to the file item but without success by using the following code.

string query = String.Format("//Games/Game/name");
XmlNodeList elements1 = xml.SelectNodes(query);
foreach (XmlNode xn in elements1)
{
   s1 = xn.InnerText;
   string query1 = String.Format("../file");
   XmlNodeList elements2 = xml.SelectNodes(query1);
   foreach (XmlNode xn2 in elements2)
   {
     s2 = xn2.InnerText;
   }
}

我希望有一个解决方案.

I hope there is a solution.

推荐答案

您可以使用 Game[name]Game 元素过滤为具有子元素 的元素名称.这是可能的,因为 child:: 是默认轴,当没有提到显式轴时将隐含.进一步扩展它以检查子元素 file,就像 Game[name and file] 一样简单:

You can use Game[name] to filter Game elements to those with child element name. This is possible because child:: is the default axes which will be implied when no explicit axes mentioned. Extending this further to check for child element file as well, would be as simple as Game[name and file] :

string query = String.Format("//Games/Game[name]");
XmlNodeList elements1 = xml.SelectNodes(query);
foreach (XmlNode xn in elements1)
{
   s1 = xn["name"].InnerText;
   s2 = xn["file"].InnerText;
}

<小时>

现在从字面上回答您的问题,您可以使用 following-sibling:: 轴来获取跟随当前上下文元素的同级元素.因此,鉴于上下文元素是 name,您可以执行 following-sibling::file 来返回同级 file 元素.


Now to answer your question literally, you can use following-sibling:: axes to get sibling element that follows current context element. So, given the context element is name, you can do following-sibling::file to return the sibling file element.

您使用 ../file 的尝试也应该有效.唯一的问题是,您的代码在 xmlXmlDocument 上执行该 XPath,而不是在当前 name 元素上执行它:

Your attempt which uses ../file should also work. The only problem was, that your code executes that XPath on xml, the XmlDocument, instead of executing it on current name element :

XmlNodeList elements2 = xn.SelectNodes("../file");

这篇关于如何获取xml节点的兄弟节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆