How to get XML into a Dictionary with an Expression?
- by DaveDev
I have the following XML:
<PerformancePanel page="PerformancePanel.ascx" title="">
<FundGroup heading="Net Life Managed Funds">
<fund id="17" countryid="N0" index="24103723" />
<fund id="81" countryid="N0" index="24103723" />
<fund id="127" countryid="N0" index="24103722" />
<fund id="345" countryid="N0" index="24103723" />
<fund id="346" countryid="N0" index="24103723" />
</FundGroup>
<FundGroup heading="Net Life Specialist Funds">
<fund id="110" countryid="N0" index="24103717" />
<fund id="150" countryid="N0" index="24103719" />
<fund id="119" countryid="N0" index="24103720" />
<fund id="115" countryid="N0" index="24103727" />
<fund id="141" countryid="N0" index="24103711" />
<fund id="137" countryid="N0" />
<fund id="146" countryid="N0" />
<fund id="133" countryid="N0" />
<fund id="90" countryid="N0" />
<fund id="104" countryid="N0" />
<fund id="96" countryid="N0" />
</FundGroup>
</PerformancePanel>
I can get the data into an anonymous object as follows:
var offlineFactsheet = new
{
PerformancePanels =
(from panel in doc.Elements("PerformancePanel")
select new PerformancePanel
{
PerformanceFunds = (from fg in panel.Elements("FundGroup")
select new
{
Heading = (fg.Attribute("heading") == null)
? ""
: (string)fg.Attribute("heading"),
Funds =
(from fund in fg.Elements("fund")
select new Fund
{
FundId = (int)fund.Attribute("id"),
CountryId = (string)fund.Attribute("countryid"),
FundIndex = (fund.Attribute("index") == null)
? null
: new Index
{
Id = (int)fund.Attribute("index")
},
FundNameAppend = (fund.Attribute("append") == null)
? ""
: (string)fund.Attribute("append")
}).ToList()
}).ToDictionary(xx => xx.Heading, xx => xx.Funds)};
I'm trying to change my code such that I can assign the dictionary directly to a property of the class I'm working in, as described in this question.
I'd like to have a Dictionary() where each header text is the key to the list of funds under it. I'm having difficulty applying the example in the linked question because that only returns a string, and this needs to return the dictionary.
This is the point that I got to before it occurred to me that I'm lost!!!:
this.PerformancePanels = doc.Elements("PerformancePanel").Select(e =>
{
var control = (PerformancePanel)LoadControl(this.OfflineFactsheetPath
+ (string)e.Attribute("page"));
control.PerformanceFunds = e.Elements("FundGroup").Select(f =>
{
List<Fund> funds = (from fund in e.Elements("fund")
select new Fund
{
FundId = (int)fund.Attribute("id"),
CountryId = (string)fund.Attribute("countryid"),
FundIndex = (fund.Attribute("index") == null)
? null
: new Index
{
Id = (int)fund.Attribute("index")
},
FundNameAppend = (fund.Attribute("append") == null)
? ""
: (string)fund.Attribute("append")
}).ToList();
string heading = (e.Attribute("heading") == null)
? ""
: (string)e.Attribute("heading");
}).ToDictionary(xx => heading, xx => Funds);
return control;
}).ToList();
Could someone point me in the right direction please? I'm not even sure if 'Expression' is the right terminology. Could someone fill me in on that too? Thanks.