With .net 3.5, there is a SyndicationFeed that will load in a RSS feed and allow you to run LINQ on it.
Here is an example of the RSS that I am loading:
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>Title of RSS feed</title>
<link>http://www.google.com</link>
<description>Details about the feed</description>
<pubDate>Mon, 24 Nov 08 21:44:21 -0500</pubDate>
<language>en</language>
<item>
<title>Article 1</title>
<description><![CDATA[How to use StackOverflow.com]]></description>
<link>http://youtube.com/?v=y6_-cLWwEU0</link>
<media:player url="http://youtube.com/?v=y6_-cLWwEU0" />
<media:thumbnail url="http://img.youtube.com/vi/y6_-cLWwEU0/default.jpg" width="120" height="90" />
<media:title>Jared on StackOverflow</media:title>
<media:category label="Tags">tag1, tag2</media:category>
<media:credit>Jared</media:credit>
<enclosure url="http://youtube.com/v/y6_-cLWwEU0.swf" length="233" type="application/x-shockwave-flash"/>
</item>
</channel>
When I loop through the items, I can get back the title and the link through the public properties of SyndicationItem.
I can't seem to figure out how to get the attributes of the enclosure tag, or the values of the media tags. I tried using
SyndicationItem.ElementExtensions.ReadElementExtensions<string>("player", "http://search.yahoo.com/mrss/")
Any help with either of these?
Your missing the namespace. Using LINQPad and your example feed:
result: url="http://youtube.com/?v=y6_-cLWwEU0"
The construct to look at is: Element(media + "player") that tells Linq to use the namespace represented by 'media' as well as the element name 'player'.
Brain damage must be setting in on my part, I thought you were using Linq. Anyway, you need to take the namespace into consideration.
This should give you an idea on how to do it:
Here is how I managed to retrieve the enclosure link from a feed using SyndicationFeed.
The output is as follows:
You can identify the enclosure link from its relationship type.
You can use a combination of LINQ and XPathNavigator to extract the syndication extensions of a feed item (based on namespace URI of the extension). For item enclosures, you will want to examine the item links collection for links that have a RelationshipType of enclosure.
Example:
Whether you're retrieving the non-XML contents of extension elements or XElement items, you might want to consider using a generic helper function like:
Depending on whether the elements are guaranteed to be there or whether you are putting this into a reusable library, you may need to add additional defensive programming.