Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
XML
 <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>



for the above code i have written Xpath like:

C#
int total=SelectSingleNode("/lines/line/qty").Value;

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.
Posted
Updated 17-Sep-12 21:11pm
v2

Have you tried sum(//qty) expression?
 
Share this answer
 
C#
//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();
 
Share this answer
 
Comments
Madhugundi 18-Sep-12 3:47am    
Thank you i got solution from ur code.
Kuthuparakkal 18-Sep-12 4:02am    
You're welcome!
plz mark answer if it helps!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900