如何使用表达式将XML转换为字典?

时间:2010-06-11 09:27:55

标签: c# linq-to-xml lambda

我有以下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>

我可以将数据导入匿名对象,如下所示:

    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)};

我正在尝试更改我的代码,以便我可以将字典直接分配给我正在使用的类的属性,如this question中所述。 我想要一个Dictionary(),其中每个标题文本是其下的资金列表的关键。我在链接问题中应用示例时遇到困难,因为它只返回一个字符串,这需要返回字典。

在我遇到失去之前,我得到了这一点!!!:

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();

有人能指出我正确的方向吗?我甚至不确定'Expression'是否是正确的术语。也有人可以填写我的内容吗?感谢。

1 个答案:

答案 0 :(得分:0)

我明白了:

this.PerformancePanels = doc.Elements("PerformancePanel").Select(e =>
 {
     var control = (PerformancePanel)LoadControl(this.OfflineFactsheetPath
                                                + (string)e.Attribute("page"));



     control.PerformanceFunds = e.Elements("FundGroup").ToDictionary(xx => (string)xx.Attribute("heading"),
                                                                     xx => xx.Elements("fund").Select(g =>
          {
              Fund fund = new Fund
                              {
                                  FundId = (int)g.Attribute("id"),
                                  CountryId = (string)g.Attribute("countryid"),
                                  FundIndex = (g.Attribute("index") == null)
                                  ? null : new Index
                                               {
                                                   Id = (int)g.Attribute("index")
                                               },
                                  FundNameAppend = (g.Attribute("append") == null)
                                     ? ""
                                     : (string)g.Attribute("append")
                              };
              return fund;
          }).ToList());

     return control;
 }).ToList();

事实证明我不需要做select(),只需要ToDictionary()方法,参数由'表达式'填充..(如果这是正确的词?!)