Be most grateful if someone could help with this. Given the XML below, I need to group by InputItem and sum the costs for all years. So the results should be
- Research Contracts, 7000
- Technical Contracts, 5000
- Hospitality, 2000
Here's the XML:
<Inputs>
<Inputs_Table_Row>
<InputItem>Research Contracts</InputItem>
<TotalYear1>1000</TotalYear1>
<TotalYear2>2000</TotalYear2>
</Inputs_Table_Row>
<Inputs_Table_Row>
<InputItem>Research Contracts</InputItem>
<TotalYear1>2000</TotalYear1>
<TotalYear2>2000</TotalYear2>
</Inputs_Table_Row>
<Inputs_Table_Row>
<InputItem>Technical Contracts</InputItem>
<TotalYear1>1000</TotalYear1>
<TotalYear2>2000</TotalYear2>
</Inputs_Table_Row>
<Inputs_Table_Row>
<InputItem>Technical Contracts</InputItem>
<TotalYear1>1000</TotalYear1>
<TotalYear2>1000</TotalYear2>
</Inputs_Table_Row>
<Inputs_Table_Row>
<InputItem>Hospitality</InputItem>
<TotalYear1>1000</TotalYear1>
<TotalYear2>1000</TotalYear2>
</Inputs_Table_Row>
</Inputs>
Here's my attempt simply to group so far, but no success:
XDocument doc = XDocument.Load(@"c:\temp\CrpTotalsSimple.xml");
var query = from c in doc.Descendants("Inputs_Table_Row")
group new
{
Item = (string)c.Element("InputItem")
}
by c.Element("InputItem")
into groupedData
select new
{
ItemName = groupedData.Key.Value
};
foreach (var item in query)
Console.WriteLine(String.Format("Item: {0}", item.ItemName));