Tuesday, January 12, 2010

Read or consume RSS Feed in Asp.net 2.0/3.5




Please visit my new Web Site WWW.Codedisplay.com



In my first article i have explained "How you can create RSS feed using Asp.net". Here in this article i will explain how one can read or consume RSS Feed from Asp.net website. If you look at google reader then you will get a basic idea how one can Reading or Consuming RSS Feed from a RSS Feed link. I don't want to teach you the necessity of reading RSS in this article. So i want to start straight forward. Since in my first article i have created a RSS Feed so that open this project again and add another page and rename it to RSSFeedReader.aspx. In my local PC the RSS Feed link is: "http://localhost:50537/StudyProject/CreateRSS.aspx". Change it by copying the URL for CreateRSS.aspx page. Ok now everything is prepared to read or consume an RSS Feed from Asp.Net web application.






First add a server side table control in your RSSFeedReader.aspx page. So the complete HTML markup will be now:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RSSFeedReader.aspx.cs" Inherits="RSSFeedReader" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>RSS Feed Reader</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        
 <table cellpadding="0" cellspacing="0">
        <tr>
        <td>
    
 <table class="NormalText" runat="server" id="tbl_Feed_Reader" cellpadding="0" cellspacing="0">
        </table>
        
 </td>
        </tr>
        </table>    

    </div>
    </form>
</body>
</html>
The complete server side code is given below for Reading & Consuming RSS feed is like below:
using System;
using System.Data;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Xml;
using System.IO;


public partial class RSSFeedReader : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        WebRequest MyRssRequest = WebRequest.Create("http://localhost:50537/StudyProject/CreateRSS.aspx");
        WebResponse MyRssResponse = MyRssRequest.GetResponse();

        Stream MyRssStream = MyRssResponse.GetResponseStream();

        // Load previously created XML Document
        XmlDocument MyRssDocument = new XmlDocument();
        MyRssDocument.Load(MyRssStream);

        XmlNodeList MyRssList = MyRssDocument.SelectNodes("rss/channel/item");

        string sTitle = "";
        string sLink = "";
        string sDescription = "";

        // Iterate/Loop through RSS Feed items
        for (int i = 0; i < MyRssList.Count; i++)
        {
            XmlNode MyRssDetail;

            MyRssDetail = MyRssList.Item(i).SelectSingleNode("title");
            if (MyRssDetail != null)
                sTitle = MyRssDetail.InnerText;
            else
                sTitle = "";

            MyRssDetail = MyRssList.Item(i).SelectSingleNode("link");
            if (MyRssDetail != null)
                sLink = MyRssDetail.InnerText;
            else
                sLink = "";

            MyRssDetail = MyRssList.Item(i).SelectSingleNode("description");
            if (MyRssDetail != null)
                sDescription = MyRssDetail.InnerText;
            else
            {
                sDescription = "";
            }

            // Now generating HTML table rows and cells based on Title,Link & Description
            HtmlTableCell block = new HtmlTableCell();
            block.InnerHtml = "<span style='font-weight:bold'><a href='" + sLink + "' target='new'>"+ sTitle + "</a></span>";
            HtmlTableRow row = new HtmlTableRow();
            row.Cells.Add(block);
            tbl_Feed_Reader.Rows.Add(row);
            HtmlTableCell block_description = new HtmlTableCell();
            block_description.InnerHtml = "<p align='justify'>" + sDescription + "</p>";
            HtmlTableRow row2 = new HtmlTableRow();
            row2.Cells.Add(block_description);
            tbl_Feed_Reader.Rows.Add(row2);
        }
    }
}

And the Output will be look like this:


So, I hope now you can Make or Create an RSS Feed as well as can read or consume RSS feed using Asp.net. Happy programming.

7 comments:

Anonymous said...

Thanks for the Great Post
Ed

Dimitri De Vos said...

works like a charm. Thanks a lot and keep up the good work!

Lauraine said...

Thanks for the share. Keep posting such kind of information on your blog. I bookmarked it for future references.
flash to html5 converter

Foong said...

Yes, agreed! Worked like charm. Perfect! Thank you.

Anonymous said...

Works like charm! But, how would you consume the image associated with a consumed item, ie the image associated with a news story?

Anonymous said...

its very useful

Pranav Singh said...

it's not working...

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash