我有一个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