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("<a href='" + link + "' target='new'>" + title + "</a>");            
    Response.Write(" ");
}

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">

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20runat%3D%22server%22%3E%0A%20%20%20%20public%20void%20GetFeed(string%20rssURL)%0A%20%20%20%20%7B%0A%09%2F%2FPut%20here%20the%20code%20descriped%20ealier%20in%20this%20section%0A%20%20%20%20%7D%0A%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />
<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");
                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.

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.