C#.Net Rss Reader

This is an article related to adding a Rss Reader to your web page. Rss reader can be used for anything like News Channels update, Movie Reviews, Stock Exchanges updates and other relative site's latest updates.

What is RSS?

Really Simple Syndication (RSS) is a lightweight XML format designed for sharing headlines and other Web content. Think of it as a distributable "What's New" for your site. Originated by UserLand in 1997 and subsequently used by Netscape to fill channels for Netcenter, RSS has evolved into a popular means of sharing content between sites (including the BBC, CNET, CNN, Disney, Forbes, Motley Fool, Wired, Red Herring, Salon, Slashdot, ZDNet, and more). RSS solves myriad problems webmasters commonly face, such as increasing traffic, and gathering and distributing news. RSS can also be the basis for additional content distribution services.

Rss Syntax ::

RSS defines an XML grammar (a set of HTML-like tags) for sharing news. Each RSS text file contains both static information about your site, plus dynamic information about your new stories, all surrounded by matching start and end tags.

Each story is defined by an tag, which contains a headline TITLE, URL, and DESCRIPTION. Here's an example:
<item>
<title>RSS Resources</title>
<link>http://www.webreference.com/authoring/languages/xml/rss/</link>
<description>Defined in XML, the Rich Site Summary (RSS) format has
quietly become a dominant format for distributing headlines on the Web.
Our list of links gives you the tools, tips and tutorials you need to get
started using RSS. 0323</description>
</item>
...

Each RSS channel can contain up to 15 items and is easily parsed using Perl or other open source software. If you want more details on creating RSS files see Jonathan Eisenzopf's excellent article in the February issue of Web Techniques. But you don't have to worry about the details, we've made it easy to create your own RSS channel with free open source scripts, all Web based. More on these later.

Once you've created and validated your RSS text file, register it at the various aggregators, and watch the hits roll in. Any site can now grab and display your feed regularly, driving traffic your way. Update your RSS file, and all the external sites that subscribe to your feed will be automatically updated.

I have made a small application for Reading Rss contents of other web urls with making use of Web Parts.

Here are the Main part of the application

User Control – Reader.ascx

<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border=0 style="width: 100%; font-size:small; color: black; font-family: Verdana;">
</HeaderTemplate>
<ItemTemplate>
<tr bgcolor="#33cccc">
<td>
<a target="article" style="text-decoration:none; color:Black;" href=<%# DataBinder.Eval(Container.DataItem, "Link") %> >
<%# DataBinder.Eval(Container.DataItem, "Title") %></a>
</td>
</tr>
<tr bgcolor="Ivory">
<td style="color:Maroon;">
<%# DataBinder.Eval(Container.DataItem, "Description")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

Code Behind - Reader.ascx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
using System.Xml;
using System.IO;

public partial class UserControls_Reader : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = GetTable("http://www.zeenews.com/rss/india-national-news.xml");
//Repeater1.DataSource = GetTable("http://www.cricinfo.com/rss/livescores.xml"); // For Cricket Live Scores.
Repeater1.DataBind();
}

public DataTable GetTable(string rssUrl)
{
WebRequest request = WebRequest.Create(rssUrl);
WebResponse response = request.GetResponse();
StringBuilder sb = new StringBuilder("");
Stream rssStream = response.GetResponseStream();
XmlDocument rssDoc = new XmlDocument();

rssDoc.Load(rssStream);
XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");

DataTable NewsTable = new DataTable();

NewsTable.Columns.Add(new DataColumn("Title", typeof(string)));
NewsTable.Columns.Add(new DataColumn("Link", typeof(string)));
NewsTable.Columns.Add(new DataColumn("Description", typeof(string)));

int upperlimit = rssItems.Count;
if (upperlimit > 10)
upperlimit = 10;
if (upperlimit > 0)
{
for (int i = 0; i < upperlimit; i++)
{
DataRow NewsTableRow = NewsTable.NewRow();

XmlNode rssDetail;
rssDetail = rssItems.Item(i).SelectSingleNode("title");
if (rssDetail != null)
{
NewsTableRow["Title"] = rssDetail.InnerText;
}
else
{
NewsTableRow["Title"] = "";
}

rssDetail = rssItems.Item(i).SelectSingleNode("link");
if (rssDetail != null)
{
NewsTableRow["Link"] = rssDetail.InnerText;
}
else
{
NewsTableRow["Link"] = "";
}

rssDetail = rssItems.Item(i).SelectSingleNode("description");
if (rssDetail != null)
{
NewsTableRow["Description"] = rssDetail.InnerText;
}
else
{
NewsTableRow["Description"] = "";
}

NewsTable.Rows.Add(NewsTableRow);
}
}
return NewsTable;
}
}

I have written the code in UserControl because, the UserControl can be directly placed anywhere in the application like as I have used it in My Web Part. I am not putting the coding part of Web Parts, as it can be found anywhere and its too long.

I have taken Repeater Control to show the messages, so the Rss contents can be seen line by line.

Thank you.


C# function similar to PHP file_get_contents

Here is a C# function that works similar to the PHP function file_get_contents. The given C# function will read the string contents of a regular file, or a file from a URL (the response from a http request, not the actual file itself).
Notice you have to convert the byte array results to ASCII using the System.Text assembly.

/// 
/// Will return the string contents of a
/// regular file or the contents of a
/// response from a URL
/// 
/// The filename or URL/// 
protected string file_get_contents(string fileName) 
{ 
    string sContents = string.Empty; 
    if (fileName.ToLower().IndexOf("http:") > -1) 
    { // URL 
         System.Net.WebClient wc = new System.Net.WebClient(); 
         byte[] response = wc.DownloadData(fileName); 
         sContents = System.Text.Encoding.ASCII.GetString(response); 
    } 
    else 
    { 
         // Regular Filename 
         System.IO.StreamReader sr = new System.IO.StreamReader(fileName); 
         sContents = sr.ReadToEnd(); 
         sr.Close(); 
    } 
    return sContents;
}

Note:
You might be able to read the actual file from the webserver if you replace the DownloadData method on the webclient object to DownloadFile, but this is purely speculation. This kind of action would be useful if you wanted the actual script file not the results from it.