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);
ds.Tables["book"]?.AsEnumerable().Sum(r => decimal.Parse((string)r["price"]))
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?First of all you read the numbers from the XML, then you add them the same way you do any other numbers.
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);
ds.Tables["book"]?.AsEnumerable().Sum(r => decimal.Parse((string)r["price"]))
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 rowsSuppose 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
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();
}
<Quantity>
<value>100</value
<sTime>2022-01-01T11:05:01am</sTime>
<sTime>2022-01-01T12:07:01pm</sTime>
</Quantity>
etc...