如何编写Xpath以提取C#中的总量 [英] How to write Xpath to extract total Quantity in C#
问题描述
我是用C#编写Xpath的新手,我无法从xml数据中获取数量总和.即我想从多个线元素中获取总量,就像以一个数据单位一样.
以下xml显示了一些示例xml数据:
Hi,
I am new in writing Xpath in C# and i am not able to get Sum of Quantity from xml data. i.e i want to get total quantity from multiple lines elements as in one unit of data.
The following xml shows some sample xml data :
<lines>
<line>
<line_no>11</line_no>
<qty>50</qty>
</line>
<line>
<line_no>12</line_no>
<qty>150</qty>
</line>
<line>
<line_no>13</line_no>
<qty>100</qty>
</line>
<line>
<line_no>14</line_no>
<qty>90</qty>
</line>
<line>
<line_no>15</line_no>
<qty>40</qty>
</line>
</lines>
对于上面的代码,我已经编写了Xpath,例如:
for the above code i have written Xpath like:
int total=SelectSingleNode("/lines/line/qty").Value;
通过这种方式,我仅获得第一行元素数量,但是我需要获取所有行数量.我认为在这里,我需要循环数据行(如果是),请帮助我如何编写xpath查询.
谢谢,
Raghu.
By this i am getting only first line element quantity but i need to get all lines quantity i think here i need to Loop the lines of data if yes please help me how to write the xpath query.
Thanks,
Raghu.
推荐答案
您是否尝试过sum(//qty)
表达式?
//Load the document
XPathDocument xDoc=new XPathDocument("XMLFilePath"));
//Create XPathNavigator
System.Xml.XPath.XPathNavigator nav = xDoc.CreateNavigator();
//Use Evaluate method to get typed result of evaluated expressions
//Here we get it as string
string sum=nav.Evaluate("sum(/lines/line/qty)").ToString();
这篇关于如何编写Xpath以提取C#中的总量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!