views:

489

answers:

5

Is it practical / possible to use serialization to read data from an RSS feed? I basically want to pull information from my Netflix queue (provided from an RSS feed), and I'm trying to decide if serialization is feasible / possible, or if I should just use something like XMLReader. Also, what would be the best way to download the feed from a URL? I've never pulled files from anything but drives, so I'm not sure how to go about doing that.

+2  A: 

If you're using .NET 3.0 or 3.5...then I would suggest using an XMLReader to read the document into an XDocument. You can then use LINQ to XML to query against and render the RSS feed into something usable.

Building something to de-serialize the XML is also feasible and will perform just as well (if not better) but will be more time intensive to create.

Either way will work...do what you're more comfortable with (or, if you're trying to learn XML serialization, go for it and learn something new).

Justin Niessner
+6  A: 

The .NET 3.5 framework added syndication support. The System.ServiceModel.Syndication namespace provides a bunch of types to manage feeds, feed content and categories, feed formatting (RSS 2.0, Atom 1.0), etc.

http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.aspx

You have a few options for serialization, but the simplest is probably best described here:

http://msdn.microsoft.com/en-us/library/bb536530.aspx

jrista
I took a look into this, but it appears the namespace only provides serialization options, not deserialization options, unless I overlooked something.
MGSoto
You "deserialize" via the SyndicationFeed.Load static method. It automatically detects the input format. If you need more control, you can use the formatters directly...all of them have a ReadFrom method that takes an XmlReader.
jrista
I'll take a look into this when I get home, this is great to know if I ever need to do another RSS project.
MGSoto
But the SyndicationFeed.Load only accepts strictly confirming feeds. It will fail (unrecoverable) on a lot of Feeds.
Henk Holterman
How are you going to hand parse a non-conforming feed? The error space is limitless :-) But I agree it is almost useless because it chokes on the almost universally bad Date strings in pubDate.
Bob Denny
You could always derive from the existing feed types and override the ReadFrom method to add custom error handling. If you really wanted to, you could write your own replacement feed types that were more forgiving, but still use the Syndication type to handle them in a format-independent way.
jrista
+5  A: 

If you can use LINQ, LINQ to XML is an easy way to get at the basics of an RSS feed document.

This is from something I wrote to select out a collection of anonymous types from my blog's RSS feed, for example:

protected void Page_Load(object sender, EventArgs e)
{
  XDocument feedXML = XDocument.Load("http://feeds.encosia.com/Encosia");

  var feeds = from feed in feedXML.Descendants("item")
              select new
              {
                Title = feed.Element("title").Value,
                Link = feed.Element("link").Value,
                Description = feed.Element("description").Value
              };

  PostList.DataSource = feeds;
  PostList.DataBind();
}

You should be able to use something very similar against your Netflix feed.

Dave Ward
+1  A: 

Check out this link for a pretty thorough download routine.

RSS is basically a derivative of XML. I like this link for defining the RSS format. This one has a really basic sample.

Brad Bruce
A: 
P.K