我有以下 rss 提要,我想查询它以获取具有名为子频道的特定类别的所有项目。到目前为止,我只设法到达第一个元素,但前提是它是列表中的第一个元素。我将如何编写 linq 查询来过滤 rss 提要以仅显示某个“子通道”与特定值匹配的项目,例如。“准确性”?
干杯,克里斯
protected void Filter()
{
var feeds = (from item in doc.Descendants("item")
where
item.CategoryValue("Channel") == "Accomplishment"
select new
{
Title = item.Element("title").Value,
rssGuid = item.Element("guid").Value,
description = item.Element("description").Value,
link = item.Element("link").Value
}).ToList();
ListView1.DataSource = feeds;
ListView1.DataBind();
}
扩展方法
public static class ccExtensions
{
public static string CategoryValue(this XElement item, string type)
{
var category = item.Elements("category").FirstOrDefault(c => (string)c.Attribute("domain") == type
&& !string.IsNullOrEmpty(c.Value));
return category == null ? null : category.Value;
}
}
RSS 提取
<item>
<title>Time Mgmt</title>
<description>The following is a list of sample values</description>
<link>http://blah.com/test.aspx</link>
<category domain="Channel"></category>
<category domain="Channel">Goals</category>
<category domain="SubChannel"></category>
<category domain="SubChannel">Accountability</category>
<category domain="SubChannel">Accomplishment</category>
<category domain="SubChannel">Achievement</category>
<category domain="SubChannel">Accuracy</category>
<category domain="SubChannel">Agility</category>
<category domain="SubChannel">Ambition</category>
<category domain="SubChannel">Anticipation</category>
<category domain="SubChannel">Appreciation</category>
<category domain="SubChannel">Assertiveness</category>
<category domain="SubChannel">Beauty</category>
<category domain="Type"></category>
<category domain="Type">Time Management</category>
<guid>5e993951-da49-400b-b8d4-68b95628b9d5</guid>
</item>