no

How to Implement a Rss Feed Reader 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...

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:

http://ph.news.yahoo.com/rss/most-popular-most-viewed-ph.xml

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

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();
            ds.ReadXml(webResponse.GetResponseStream());

            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")
                                    }).ToList();
            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:
RssReader.Read("http://ph.news.yahoo.com/rss/most-popular-most-viewed-ph.xml");

Related

c# 2493682368051281020

Post a Comment Default Comments

2 comments

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.

item