Compartir a través de


Consume an RSS Feed with .NET 3.5

.NET 3.5 added some new classes that really help when dealing with RSS/ATOM feeds.  These classes are located in the System.ServiceModel.Syndication namespace in the System.ServiceModel.Web.dll library.  The SyndicationFeed class in this namespace can be used to both expose and consume a feed.

Today I was attempting to create a clone of my feed on a website that I am toying around with.  After doing some research on the System.ServiceModel.Syndication namespace, I found that this is extremely simple to do in 3.5.  The following code illustrates just how simple it can be by using the static Load method of SyndicationFeed:

    1: // Blog feed
    2: SyndicationFeed blogFeed;
    3: try
    4: {
    5:     // Read the feed using an XmlReader
    6:     using (XmlReader reader = XmlReader.Create("https://someblog/feed.xml");
    7:     {
    8:         // Load the feed into a SyndicationFeed
    9:         blogFeed = SyndicationFeed.Load(reader);
   10:     }
   11: }
   12: catch (Exception ex)
   13: {
   14:     if (ex is WebException || ex is XmlException)
   15:     {
   16:         // Handle bad url, timeout or xml error here.
   17:     }
   18:     else
   19:         throw;
   20: }
   21:  
   22: // Use the feed
   23: foreach (SyndicationItem item in blogFeed.Items)
   24: {
   25:     Console.WriteLine(item.Title.Text);
   26: }

Comments

  • Anonymous
    July 25, 2008
    PingBack from http://blog.a-foton.ru/2008/07/consume-an-rss-feed-with-net-35/

  • Anonymous
    July 28, 2008
    Excellent thanks for this. Saved me about 2 hours of writing RSS data adapters. !mikedopp

  • Anonymous
    December 08, 2008
    your are my hero today, thanks.

  • Anonymous
    April 17, 2009
    Right on.  Nice work.  Saved me a bunch of time as well.

  • Anonymous
    November 13, 2009
    Wow, am I late to the party!  Thanks!  This is exactly what I was looking for.

  • Anonymous
    December 22, 2013
    I got the error , The name reaer does not exists the current context.