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