c# 解析XML文档,怎么循环获取每个TASK_ID的值,并且统计BOX_NUM,如:box_num统计后得到 8,感激不尽
<?xmlversion='1.0'encoding='UTF-8'?><DBSETRESULT="1"><R><CN="BOX_NUM">2</C><CN="TASK_...
<?xml version='1.0' encoding='UTF-8'?>
<DBSET RESULT="1">
<R>
<C N="BOX_NUM">2</C>
<C N="TASK_ID">1739387940</C>
</R>
<R>
<C N="BOX_NUM">6</C>
<C N="TASK_ID">1739387942</C>
</R>
</DBSET>
不要下面这种解决方案:
XmlNodeList nodes = objXmlDoc.SelectNodes("//R");
int num=0;
foreach (XmlNode node in nodes)
{
String box_num = node.GetElementsByTagName("C")[0].InnerText;
num=num+Convert.toint32(box_num);
String tid = node.GetElementsByTagName("C")[1].InnerText;
}
我想通过<C N='TASK_ID'>指定的N来获取,谢谢 展开
<DBSET RESULT="1">
<R>
<C N="BOX_NUM">2</C>
<C N="TASK_ID">1739387940</C>
</R>
<R>
<C N="BOX_NUM">6</C>
<C N="TASK_ID">1739387942</C>
</R>
</DBSET>
不要下面这种解决方案:
XmlNodeList nodes = objXmlDoc.SelectNodes("//R");
int num=0;
foreach (XmlNode node in nodes)
{
String box_num = node.GetElementsByTagName("C")[0].InnerText;
num=num+Convert.toint32(box_num);
String tid = node.GetElementsByTagName("C")[1].InnerText;
}
我想通过<C N='TASK_ID'>指定的N来获取,谢谢 展开
2个回答
展开全部
using (var reader = CreateXmlReader())
{
var document = new XPathDocument(reader);
var xPathNavigator = document.CreateNavigator();
//所有TASK_ID值列表
var taskIdNodes = xPathNavigator.Select("//C[@N='TASK_ID']/text()");
var taskIds = (from XPathNavigator taskIdNode in taskIdNodes select taskIdNode.Value).ToArray();
// 所有BOX_NUM值列表
var boxNumNodes = xPathNavigator.Select("//C[@N='BOX_NUM']/text()");
var boxNums = (from XPathNavigator boxNumNode in boxNumNodes select int.Parse(boxNumNode.Value)).ToArray();
// 计算累加值
var sum = 0;
Array.ForEach(boxNums, boxNum => sum += boxNum);
// 在调试窗口输出结果
Debug.WriteLine("task id:" + string.Join(", ", taskIds));
Debug.WriteLine("box number:" + string.Join(", ", boxNums));
Debug.WriteLine("sum:" + sum);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没太看明白,是这样么?
XmlNodeList nodes = objXmlDoc.SelectNodes("//R");
int num=0;
foreach (XmlNode node in nodes)
{
String box_num = node.SelectSingleNode("C[@N='BOX_NUM']").InnerText;
num=num+Convert.ToInt32(box_num);
String tid = node.SelectSingleNode("C[@N='TASK_ID']").InnerText;
}
XmlNodeList nodes = objXmlDoc.SelectNodes("//R");
int num=0;
foreach (XmlNode node in nodes)
{
String box_num = node.SelectSingleNode("C[@N='BOX_NUM']").InnerText;
num=num+Convert.ToInt32(box_num);
String tid = node.SelectSingleNode("C[@N='TASK_ID']").InnerText;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询