Adding daily numbers from xml file to produce a total

cp713a

Member
Joined
Jul 26, 2022
Messages
7
Programming Experience
Beginner
Hi,

I'm new to this forum and I have a question: how do I sum up all the values in a XML file to get the total?

Any help would be appreciate it,
 
Solution
Suppose I downloaded the xml file from Sample XML File (books.xml) to c:\temp\a.xml

I could sum the prices therein with a new .net 6 project having just the following lines in its Program.cs:

C#:
using System.Data;

var ds = new DataSet();
ds.ReadXml(@"C:\temp\a.xml");
var sumPrice = ds.Tables["book"]?.AsEnumerable().Sum(r => decimal.Parse((string)r["price"]));

Console.WriteLine(sumPrice);

There are, of course, many ways to skin this cat but you didn't actually show us any part of your existing cat.. :)

The part that does the summing is:

ds.Tables["book"]?.AsEnumerable().Sum(r => decimal.Parse((string)r["price"]))

On its own, a datatable is a particularly ancient device and doesn't play well with LINQ, which is...
First of all you read the numbers from the XML, then you add them the same way you do any other numbers.
Thanks for the reply. My program can read the numbers from the XML, but I don't know how to write to sum them all up? Can you provide a sample on how to iterate and sum them all up?
 
Okay now that is amazing: you can read an XML file, but you can't add numbers.

What C# book or tutorial did you use to learn C#? One of the earliest steps of learning C# is how to use variables and doing math operations on them. Later in your C# learning you would have next covered arrays. One of the most common lessons when dealing with arrays is going through the array and adding up all the values in it. Then later the lessons will typically move on to other data structures like lists and collections, and show how similar operations can be done. Then usually the teaching progression then moves on to how to do file I/O. And then only in advanced lessons do they teach how to parse files other than plain text files. The fact that you are doing advanced parsing of XML makes things very odd.
 
Suppose I downloaded the xml file from Sample XML File (books.xml) to c:\temp\a.xml

I could sum the prices therein with a new .net 6 project having just the following lines in its Program.cs:

C#:
using System.Data;

var ds = new DataSet();
ds.ReadXml(@"C:\temp\a.xml");
var sumPrice = ds.Tables["book"]?.AsEnumerable().Sum(r => decimal.Parse((string)r["price"]));

Console.WriteLine(sumPrice);

There are, of course, many ways to skin this cat but you didn't actually show us any part of your existing cat.. :)

The part that does the summing is:

ds.Tables["book"]?.AsEnumerable().Sum(r => decimal.Parse((string)r["price"]))

On its own, a datatable is a particularly ancient device and doesn't play well with LINQ, which is the much newer technology doing the summing.

ds.Tables["book"]?.AsEnumerable() makes the datatable play nicely with LINQ; if you've read your data into some other collection youre unlikely to need it.

.Sum(r => decimal.Parse((string)r["price"])) uses LINQ to sum; you call it on an enumerable collection; Sum will visit every item in the collection, and you specify what to do by giving a name for the item e.g. r which, in this case, is a datarow. Sum expects you to write some code after the => that returns a numeric value. In this case I pull the "price" string out of the datarow and parse it to a decimal. Sum will add up together all the decimal prices in all the rows
 
Last edited:
Solution
Suppose I downloaded the xml file from Sample XML File (books.xml) to c:\temp\a.xml

I could sum the prices therein with a new .net 6 project having just the following lines in its Program.cs:

C#:
using System.Data;

var ds = new DataSet();
ds.ReadXml(@"C:\temp\a.xml");
var sumPrice = ds.Tables["book"]?.AsEnumerable().Sum(r => decimal.Parse((string)r["price"]));

Console.WriteLine(sumPrice);

There are, of course, many ways to skin this cat but you didn't actually show us any part of your existing cat.. :)

The part that does the summing is:

ds.Tables["book"]?.AsEnumerable().Sum(r => decimal.Parse((string)r["price"]))

On its own, a datatable is a particularly ancient device and doesn't play well with LINQ, which is the much newer technology doing the summing.

ds.Tables["book"]?.AsEnumerable() makes the datatable play nicely with LINQ; if you've read your data into some other collection youre unlikely to need it.

.Sum(r => decimal.Parse((string)r["price"])) uses LINQ to sum; you call it on an enumerable collection; Sum will visit every item in the collection, and you specify what to do by giving a name for the item e.g. r which, in this case, is a datarow. Sum expects you to write some code after the => that returns a numeric value. In this case I pull the "price" string out of the datarow and parse it to a decimal. Sum will add up together all the decimal prices in all the rows

Here's what I have so far:
C#:
     class Program {
     const string filename = @"c:\temp\test.xml";
     static void Main(string[] args) {
    XmlDocument xdoc = new XmlDocument();
    XmlNodeList values = xdoc.GetElementsbyTagName("value");
    XmlNodeList  st = xdoc.GetElementsbyTagName("sTime");
    XmlNodeList vt = xdoc.GetElementsbyTagName("eTime");

    foreach (XmlNode value in values){
    Console.WriteLine(value.ChildNodes[0].Value);
    Console.ReadKey();
   }
---test file:
C#:
   <Quantity>  
   <value>100</value
  <sTime>2022-01-01T11:05:01am</sTime>
  <sTime>2022-01-01T12:07:01pm</sTime>
  </Quantity>
   etc...
Hope this helps
 
Last edited by a moderator:
Add another variable before the foreach loop which will hold your running sum.
Change your line 10 to parse the value and put that into variable.
Add the another line to add that variable into the running sum.
 

Latest posts

Back
Top Bottom