如何用XPATH解析XML [英] How parse XML with XPATH

查看:81
本文介绍了如何用XPATH解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个按钮 - 上一个和下一个。当我运行程序时,它必须向我显示第一个问题,答案是Qtextbox1中的问题。 Atextbox1,Atextbox2,Atextbox3 ... / Key [1]中的答案向我展示了第一个节点。但我不知道如何定义节点数。

I have two button - prev and next. When i run program it must show me first question with answers.Question in Qtextbox1. Answers in Atextbox1,Atextbox2,Atextbox3 ... /Key[1] show me first node. But i dont know how define count of nodes.

 <Key>
  first question
  <Value>answer1</Value> 
  <Value>answer2</Value> 
  <Value>answer3</Value> 
  <Value>answer4</Value> 
  <Value>answer5</Value> 
  </Key>
- <Key>
  second question
  <Value>answer1</Value> 
  <Value>answer2</Value> 
  <Value>answer3</Value> 
  </Key>
- <Key>
  third question
  <Value>answer1</Value> 
  <Value>answer2</Value> 
  <Value>answer3</Value> 
  </Key>

推荐答案

请从这里开始: http://msdn.microsoft.com/en-us/library/system.xml.xpath.aspx [ ^ ]。



< dd> -SA
Please start from here: http://msdn.microsoft.com/en-us/library/system.xml.xpath.aspx[^].

—SA


XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml("Your XML here");
XmlNodeList xnodes = xdoc.SelectNodes("/Key");
int count = xnodes.Count;
foreach (XmlNode xnode in xnodes)
{
     // TODO: Implement your code here
}


您可以使用以下xpath来获取xml中Value节点的计数。



You may use following xpath to get the count of ''Value'' node in the xml.

count(//Key/Value)





要获取节点中子元素的数量,我们可以使用以下xpath。 br $>




To get the number of child elements in a node, we may use following xpath.

count(//Key/*)


这篇关于如何用XPATH解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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