C# 在中继器中使用linq的嵌套xml
我有一个xml文件,看起来像这样,我正在尝试获取表单元格中的所有位置属性。我设法获得了标题和描述,但不知何故未能获得事件中的所有位置 有人能帮我吗 干杯 特里 到目前为止,我得出的结论如下C# 在中继器中使用linq的嵌套xml,c#,xml,linq,C#,Xml,Linq,我有一个xml文件,看起来像这样,我正在尝试获取表单元格中的所有位置属性。我设法获得了标题和描述,但不知何故未能获得事件中的所有位置 有人能帮我吗 干杯 特里 到目前为止,我得出的结论如下 var qListCurrentMonth = (from feed in doc.Descendants("item") select new { tit
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>
项目描述
http://somelink
时装秀2
项目描述
http://somelink
这里是中继器
<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>
我猜您得到的是泛型列表类型,而不是元素值。这是因为Eval返回ToString()结果,列表上的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)
});
var qListCurrentMonth=
(来自馈入文档子体(“项目”)
选择新的
{
title=feed.Element(“title”).Value,
description=feed.Element(“description”).值,
事件=
(来源于电动汽车进料元件(“事件”)。元件(“位置”)
选择ev.属性(“城市”).值)。聚合((x,y)=>x+“
”+y)
});
聚合方法将在单个实例中累积集合。如果平均每行有5个以上的事件,出于性能原因(我相信您了解字符串与StringBuilder以及性能),最好将StringBuilder用作累加器(聚合会过载)
由于您使用的是.NET3.5,我建议您使用ListView而不是Repeater。。。像往常一样。在这种情况下,GridView可能更好,因为您代表的是一个表
最后-如果您只想在“事件”单元格中列出城市代码,请遵循.NET约定并使用PascalCase作为属性名称(即事件而非事件):
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)
});
Stillgar的聚合方法也是一个很好的建议。您考虑过使用XSLT吗?:-)我无法想象如果一个人可以使用LINQ到XML,他会想使用XSLT。也就是说,XSLT有正当的理由,比如需要与非.NET系统进行互操作时,以及以后应该向系统提供转换时,允许LINQ转换为XML可能会带来安全风险,因为它可以用于注入任意代码。非常感谢Stillgar,我从未想过使用聚合方法。