RSS feed reader implementation in C#

Included in this page:
1.) Rss feed model (which you can customized according to your needs)
2.) Rss feed reader

My Objective is to read an rss feed of a news site like google and yahoo:

For example we want to read the RSS feed of yahoo news in the Philippines:

Note that the model also applies to google news since they have almost the same xml structure:
  <pubDate>Publication date</pubDate>

First we need to setup a model where we will store the feed. Let's call this class RSSNews:
public class RssNews
        public string Title;
        public string PublicationDate;
        public string Description;

Then you need a class to read a rss feed url:
using System.Collections.Generic;
using System.Net;
using System.Data;
using System.Linq;

public class RssReader
        public static List<rssnews> Read(string url)
            var webResponse = WebRequest.Create(url).GetResponse();
            if (webResponse == null)
                return null;
            var ds = new DataSet();

            var news = (from row in ds.Tables["item"].AsEnumerable()
                         select new RssNews
                                         Title = row.Field<string>("title"),
                                        PublicationDate = row.Field<string>("pubDate"),
                                        Description = row.Field<string>("description")
            return news;
*Make sure you include all the imports.

And finally, the code to call the reader, which you can place in your Page_Load event if you are working on a web app else Main method:
RSS feed reader implementation in C# RSS feed reader implementation in C# Reviewed by Edward Legaspi on Thursday, January 20, 2011 Rating: 5


Sean said...

Just curious. How would I output this content to a DIVs innerhtml for display? Any help would be great.

Anonymous said...

There are many ways, depends on how you want to achieve it. If it's at the same time the form loads, then just post the records in a model list and display it on the div. Or it can be through jquery call.

Powered by Blogger.