【发布时间】:2009-08-01 12:44:10
【问题描述】:
我有一个看起来像这样的 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 吗? :-)
-
如果他可以使用 LINQ to XML,我无法想象一个人会想要使用 XSLT 的情况。话虽这么说,XSLT 有正当的理由,比如何时需要与非 .NET 系统进行互操作,何时应在稍后向系统提供转换,并且允许 LINQ to XML 可能存在安全风险,因为它可以使用注入任意代码。