2010-06-11 57 views
0

我有以下XML:如何使用表達式將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(); 

有人能指出我在正確的方向嗎?我甚至不確定'表達'是否是正確的術語。有人能讓我滿意嗎?謝謝。

回答

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()方法,用參數由「表達式」充滿​​.. (如果這是正確的話?!)