65.9K
CodeProject is changing. Read more.
Home

User control to display RSS channels

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.33/5 (3 votes)

Nov 10, 2006

CPOL

2 min read

viewsIcon

28286

downloadIcon

326

Add news content to any ASPX page, and configure the control easily.

Sample Image - rssreader.gif

Introduction

This user control displays one or more RSS channels in an HTML table. It also includes a method (not used in the example) to download the content of the channel for later use. The difference between this control and other controls or services like rss-info is that the appearance of the control can be configured with the same CSS styles used in the rest of your application.

Using the Code

Include the user control in your app and add it to the desired page.

The control has these properties:

MainTableCss Name of the CSS style to apply to the table which contains all the feeds.
ChannelCss Name of the CSS style to apply to the name of the RSS channel.
LinkCss Name of the CSS style to apply to the link associated with the current news.
LinkCellCss Name of the CSS style to apply to the cell which contains the news title.
ContentCss Name of the CSS style to apply to the cell which contains the text of the news.
showContent If this flag is true, the content of the news will be displayed; otherwise, only the title and link will be displayed. The default value is false.
width Width of the table which contains all the feeds (pixels); if it is not specified, the default value is 100.
urls String array which contains all the RSS channel URLs to show.
newsNumber Number of news to show per channel; if it is not specified, the control will display all the news.
showContentInHtml If the flag is true, the content of the news will be displayed as HTML; otherwise, it will be displayed in text format. The default format is text.

And a method:

DownloadFeed This function returns all the data returned to the browser by a URL. This can be an HTML page or whatever; you can use this string to save the content to a file.

Example use:

//This code is in the example aspx.cs page

string [] channels= new string[3];
channels[0] = "http://rssfeeds.usatoday.com/usatoday-HorseRacing";
channels[1] = "http://www.horse-races.net/HRN-rssfeed.xml";
channels[2] = "http://news.google.com.mx/nwshp?client=firefox-a&rls" + 
              "=org.mozilla:es-ES:official&ie=UTF-8&oe=UTF-8&hl=" + 
              "es&client=firefox-a&rls=org.mozilla:es-ES:" + 
              "official_s&tab=wn&q=&output=rss";

Rssfeed1.urls = channels;
Rssfeed1.width = 150;
Rssfeed1.showContent = true;
Rssfeed1.showContentInHtml = true;
Rssfeed1.ChannelCss = "GrayBackgroundActive";
Rssfeed1.ContentCss = "NormalLetter";
Rssfeed1.LinkCss = "projectletter";
Rssfeed1.newsNumber = 3;

Points of Interest

The control uses a dataset to populate the table, and that dataset is filled directly from the RSS channel with the method ReadXml(), but some channels throw an error because the item "link" is repeated in the same node; when this happens, you will see an error.

In this case, you could use the method DownloadFeed of the control to download the content of the channel and parse it as you wish. But the control works fine with most RSS channels; in the sample code, the Google News channel and two othres are used.