Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
At the bottom I have pasted in an XML file I am working with.   My goal is to query this file using LINQ to XML and have the response formatted as follows:

reportDate            alias24         alias39
2009-05-27         138,385.11      2,026.49
2009-05-28         138,535.98      2,233.74
2009-05-29         138,825.29      2,637.20

As you can see, I basically just want the total column for each account listed side by side. However, I'm not sure how to do this.   The closest I've been able to come is getting something that looks like this (with the 0 being the enumerated value given to the first alias and 1 being
the enumerated value given to the second alias).

2009-05-27   0
2009-05-27   1
2009-05-28   0
2009-05-28   1
2009-05-29   0
2009-05-29   1

But I'm not sure how I would combine the rows for each date...and then have the total listed.

Can any LINQ to XML gurus out there help?

Here's the file...
-------------------------->
<FlexQueryResponse queryName="TestingXML" type="AF">
<Message>The following accounts were excluded from this statement: U69</Message>
<FlexStatements count="0">
<FlexStatement accountId="U24" fromDate="20090501" toDate="20090529">
<AccountInformation acctAlias="alias24" accountId="U24" />
<EquitySummaryInBase>
<EquitySummaryByReportDateInBase reportDate="2009-05-27" total="138,385.11" />
<EquitySummaryByReportDateInBase reportDate="2009-05-28" total="138,535.98" />
<EquitySummaryByReportDateInBase reportDate="2009-05-29" total="138,825.29" />
</EquitySummaryInBase>

</FlexStatement><FlexStatement accountId="U39" fromDate="20090501" toDate="20090529">
<AccountInformation acctAlias="alias39" accountId="U39" />
<EquitySummaryInBase>
<EquitySummaryByReportDateInBase reportDate="2009-05-27" total="2,026.49" />
<EquitySummaryByReportDateInBase reportDate="2009-05-28" total="2,233.74" />
<EquitySummaryByReportDateInBase reportDate="2009-05-29" total="2,637.20" />
</EquitySummaryInBase>

</FlexStatement>
</FlexStatements>
</FlexQueryResponse>
Posted

1 solution

You should first make a query for getting all totals for each alias and date:
var flatteningQuery = from statement in doc.Root.Element("FlexStatements").Descendants("FlexStatement")
                      from summary in statement.Element("EquitySummaryInBase").Descendants("EquitySummaryByReportDateInBase")
                      select new
                      {
                          Alias = statement.Element("AccountInformation").Attribute("acctAlias").Value,
                          Date = summary.Attribute("reportDate").Value,
                          Total = summary.Attribute("total").Value
                      };
Then, you can group the result from the previous query for each date, getting the list of all alias-total pair corresponding to report dates:
var groupedQuery = from row in flatteningQuery
                   group row by row.Date into dateGroup
                   select new
                   {
                       Date = dateGroup.Key,
                       Values = from item in dateGroup
                                select new
                                {
                                    Alias = item.Alias,
                                    Total = item.Total
                                }
                   };
 
Share this answer
 
v2


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