This code is all you need:
static void Main(string[] args) {
String xml = @"
<transactions>
<transaction name=""Fred"" amount=""5,20"" />
<transaction name=""John"" amount=""10,00"" />
<transaction name=""Fred"" amount=""3,00"" />
</transactions>";
XDocument xmlDocument = XDocument.Parse(xml);
var query = from x in xmlDocument.Descendants("transaction")
group x by x.Attribute("name").Value into g
select new { Name = g.Key, Amount = g.Sum(t => Decimal.Parse(t.Attribute("amount").Value)) };
foreach (var item in query) {
Console.WriteLine("Name: {0}; Amount: {1:C};", item.Name, item.Amount);
}
}
And the content is:
Name: Fred; Amount: R$ 8,20;
Name: John; Amount: R$ 10,00;
That is the way of doing this in C# - in a declarative way!
I hope this helps,
Ricardo Lacerda Castelo Branco