Building a RSS reader in ASP.Net

Hi

RSS Feeds are just XMLs – so building a RSS reader is not so difficult, first you know how to do it ;-)

Let’s start with looking at C# function that reads the RSS Feeds.

First step is to collect the feeds. This is done by using WebRequest:

System.Net.WebRequest myRequest = System.Net.WebRequest.Create(rssURL);
System.Net.WebResponse myResponse = myRequest.GetResponse();
 
System.IO.Stream rssStream = myResponse.GetResponseStream();
System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();
rssDoc.Load(rssStream);

Now that we have collected the feeds, we are able to process the XML (rssDoc). In my example I would like to run though the items and then write the title.

This would look something like this:

System.Xml.XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
 
string title = "";
string link = "";
 
for (int i = 0; i < rssItems.Count; i++)
{            
    title    = rssItems.Item(i).SelectSingleNode("title").InnerText;
    link     = rssItems.Item(i).SelectSingleNode("link").InnerText;            
 
    Response.Write("<p><a href='" + link + "' target='new'>" + title + "</a><br/>");
 
    Response.Write("</p>");
}

That’s all, now you have a function that can collect the Feeds.

Please Notice – not all Feeds have the same XML format. They can/will differ from Feed provider to Feed provider.

Now lets take a closer look on the ASP.Net page.

<%@ Page Language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
    public void GetFeed(string rssURL)
    {
	//Put here the code descriped ealier in this section
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>mibuso Feeds</title>    
</head>
<body style ="font-family: Tahoma,Arial,Verdana,Helvetica;">
    <form id="form1" runat="server">
        <div>        
            <%                
                rssUrl = "http://www.mibuso.com/forum/smartfeed.php?forum=23&firstpostonly=1&limit=NO_LIMIT&count_limit=10&sort_by=postdate_desc&feed_type=RSS2.0&feed_style=HTML";
 
                Response.Write("mibuso Feeds<br />");
                GetFeed(rssUrl);
            %>
        </div>
    </form>
</body>
</html>

Here we assume, that the above C# function is called GetFeed. This function must be added in a script section and can afterwards be called from the body section.

Next you place the asp.net page on the internet server. When calling it – you should get a result like this:
RssFeeds-AspNet

That’s all, now you have a ASP.Net page, that collects the feeds and show the title.

You can skip to the end and leave a response. Pinging is currently not allowed.

Leave a Reply


+ five = 11