J'ai le flux rss suivant que je voudrais interroger pour obtenir tous les éléments avec une catégorie spécifique sous-canal nommé. Jusqu'ici, je n'ai réussi à atteindre le premier élément que si c'est le premier dans la liste. Comment j'écrirais la requête linq pour filtrer le flux rss afin d'afficher uniquement les éléments où un certain "sous-canal" correspond à une valeur spécifique, par exemple. "Précision"?lire les articles rss avec linq
applaudissements, Chris
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();
}
Méthode d'extension
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 extrait
<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>
Merci Jon, vous avez sauvé ma journée. Fonctionne comme le charme – Chris