C# 使用linq阅读rss项目

C# 使用linq阅读rss项目,c#,linq-to-xml,C#,Linq To Xml,我有下面的rss提要,我想查询它,以获取具有名为subchannel的特定类别的所有项目。 到目前为止,我只找到了第一个元素,但前提是它是列表中的第一个元素。 我如何编写linq查询来过滤rss提要,以便只显示某个“子频道”与特定值(例如“准确性”)匹配的项目 干杯, 克里斯 扩展方法 public static class ccExtensions { public static string CategoryValue(this XElement item, string type)

我有下面的rss提要,我想查询它,以获取具有名为subchannel的特定类别的所有项目。 到目前为止,我只找到了第一个元素,但前提是它是列表中的第一个元素。 我如何编写linq查询来过滤rss提要,以便只显示某个“子频道”与特定值(例如“准确性”)匹配的项目

干杯, 克里斯

扩展方法

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>

时间管理
以下是示例值列表
http://blah.com/test.aspx
目标
责任
成就
成就
精确
敏捷性
志向
期待
欣赏
自信
美女
时间管理
5e993951-da49-400b-b8d4-68b95628b9d5

问题在于,尽管您有多个类别,但CategoryValue返回的是单个字符串。我建议您将其更改为:

public static class ccExtensions
{
    public static IEnumerable<string> Categories(this XElement item,
                                                 string type)
    {
        return from category in item.Elements("category")
               where (string) category.Attribute("domain") == type
                     && !string.IsNullOrEmpty(category.Value)
               select category.Value;
    }
}

谢谢乔恩,你救了我一天。像查理一样工作
public static class ccExtensions
{
    public static IEnumerable<string> Categories(this XElement item,
                                                 string type)
    {
        return from category in item.Elements("category")
               where (string) category.Attribute("domain") == type
                     && !string.IsNullOrEmpty(category.Value)
               select category.Value;
    }
}
from item in doc.Descendants("item")
where item.Categories("SubChannel").Contains("Accomplishment")
select ...