2009-08-01 55 views
1

我有一個xml文件,看起來像這樣,我試圖獲取表格單元格中的所有位置屬性。我設法得到標題和描述,但不知何故無法獲得事件中的所有位置。與linq在中繼器嵌套的XML

有人可以幫我解決這個問題嗎?

乾杯 特里

我想出迄今已是以下

var qListCurrentMonth = (from feed in doc.Descendants("item") 
         select new 
         { 
          title = feed.Element("title").Value, 
          description = feed.Element("description").Value, 
          events = (from ev in feed.Element("events").Elements("location") 
             select new 
               { 
                city = ev.Attribute("city") 
               }).ToList() 
         }); 

rptFeedItems.DataSource = qListCurrentMonth; 
rptFeedItems.DataBind(); 

這裏的XML

活動 時裝秀1

<description>item descr</description> 
    <link>http://somelink</link> 

    <events> 
    <location city="nyc" date="12.12.08" link="http://www.etc.com" /> 
    <location city="nyc" date="25.11.08" link="http://www.etc.com" /> 
    <location city="sfo" date="11.11.08" link="http://www.etc.com" /> 
    <location city="sfo" date="22.01.08" link="http://www.etc.com" /> 
    <location city="dal" date="12.12.08" link="http://www.etc.com" /> 

    </events> 
</item> 

<item> 
    <title>Fashion show 2</title> 

    <description>item descr</description> 
    <link>http://somelink</link> 

    <events> 
    <location city="nyc" date="12.12.08" link="http://www.etc.com" /> 
    <location city="nyc" date="25.11.08" link="http://www.etc.com" /> 
    <location city="sfo" date="11.11.08" link="http://www.etc.com" /> 
    <location city="sfo" date="22.01.08" link="http://www.etc.com" /> 
    <location city="dal" date="12.12.08" link="http://www.etc.com" /> 

    </events> 
</item> 

這裏的中繼

<table border="1"> 
      <asp:Repeater runat="server" ID="rptFeedItems"> 
       <ItemTemplate> 
        <tr> 
         <td><%# Eval("title")%></td> 
         <td><%# Eval("description")%></td> 
         <td><%# Eval("events")%></td> 
        </tr> 

       </ItemTemplate> 
      </asp:Repeater> 

     </table> 
+1

您是否考慮過使用XSLT? :-) – 2009-08-01 12:57:02

+0

我無法想象一個人如果可以使用LINQ to XML就想使用XSLT的情況。這就是說,XSLT有合法的理由,比如當需要與非.NET系統互操作時,以及稍後應該向系統提供轉換時,並且允許LINQ to XML由於可以使用而導致安全風險時注入任意代碼。 – Stilgar 2009-08-01 16:48:25

回答

1

我猜你所得到的泛型列表類型而不是元素值。這是因爲Eval返回ToString()結果而List上的ToString()返回該類型。有幾件事你可以做。一個是嵌套另一箇中繼器並將其綁定到events屬性。這是理論上最乾淨的解決方案,雖然在這種情況下我懷疑這是值得的。

你可以做的另一件事是積累events屬性作爲一個字符串。它可以這樣完成:

var qListCurrentMonth = 
    (from feed in doc.Descendants("item") 
    select new 
    { 
     title = feed.Element("title").Value, 
     description = feed.Element("description").Value, 
     events = 
      (from ev in feed.Element("events").Elements("location") 
      select ev.Attribute("city").Value).Aggregate((x,y) => x+ "<br />" + y) 
     }); 

Aggregate方法將在一個實例中累積集合。如果你的平均每條線有5個以上的事件,你最好使用StringBuilder作爲累加器(有Aggregate的重載),這是出於性能方面的原因(我相信你知道string vs StringBuilder和性能)。

既然你使用的是.NET 3.5,我會建議使用ListView而不是Repeater ...就像往常一樣。同樣在這種情況下,GridView可能會更好,因爲您代表的是表格。

最後 - 請遵循.NET約定,並使用PascalCase的屬性名稱(即事件,而不是事件)

1

如果你只是想列出您的活動單元格的城市代碼:

var qListCurrentMonth = 
    from feed in doc.Descendants("item") 
    let cities = 
     (from location in feed.Element("events").Elements("location") 
     select location.Attribute("city").Value) 
     .ToArray()) 
    select new 
    { 
     title = feed.Element("title").Value, 
     description = feed.Element("description").Value, 
     events = string.Join(", ", cities) 
    }); 

Stilgar的Aggregate方法也是一個很好的建議。