Friday, September 23, 2011

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


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.


<%@ 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>


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);
        }
    }
}

No comments :

Post a Comment