How to consume RSS FEED in ASP.NET 3.5 in C#



In this article I am trying to use RSS in a very easy manner. Two forms are used here. In the first form, there is a text box and a button. In the text box you will enter a RSS field name and after clicking the button, the next page will show the RSS feed. I used a Cookie in the first page which will take the values from the text Box. In the second page the Load Event cookie value is shown in a label box and in the dataset the RSS will be seen.

TextValue Page is First Page:

RSS1.gif

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TextValue Entry.aspx.cs" Inherits="TextValue_Entry" %>

<!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>Untitled Page</title>
    <style type="text/css">
        #form1 {
            width: 296px;
        }
    </style>
</head>
<
body>
    <form id="form1" runat="server">
    <div>

        <asp:TextBox ID="TextBox1" runat="server" Width="553px"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
   
    </div>
    </form>
</body>
</
html>

Button _Click() Code for TextValue page:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class TextValue_Entry : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpCookie ck = new HttpCookie("temp",TextBox1.Text);
        Response.Cookies.Add(ck);
        ck.Expires = DateTime.Now.AddMonths(10);
        Response.Redirect("Default.aspx");
        //Response.Redirect(TextBox1.Text);
    }
}

Default Page is Second Page:

RSS2.gif

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Untitled Page</title>
    <style type="text/css">
        #form1 {
            width: 210px;
        }
    </style>
</head>
<
body>
    <form id="form1" runat="server">
 <div>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server"
           
DataFile= "http://rss.news.yahoo.com/rss/topstories;"
            XPath="rss/channel/item" ontransforming="XmlDataSource1_Transforming1"></asp:XmlDataSource>

        <asp:Label ID="Label1" runat="server" Text="||||||||||||||||||||||||"
           Font-Bold="True"></asp:Label>

     </div>
        <asp:DataList ID="DataList1" runat="server"
     DataSourceID="XmlDataSource1" BackColor="White" BorderColor="#404040"
     BorderStyle="Solid" GridLines="Vertical" Height="286px">
            <ItemTemplate>
                <%#XPath("title")%><br />
                <%#XPath("pubDate")%><br />
                <%#XPath("author")%><br />
                <%#XPath("description")%>
            </ItemTemplate>
            <AlternatingItemStyle BackColor="#FF8AFF" />
            <ItemStyle BackColor="#C1FF84" ForeColor="Black" />
            <HeaderStyle BackColor="#8CA9FF" ForeColor="White" Font-Bold="true" />
        </asp:DataList>
    </form>

</body>
</
html>

What you are entering in the TextBox value in the text value page (I entered this value http://rss.news.yahoo.com/rss/topstories); after running the program, that must be same as in the following area in the Default Page source Code:

<asp:XmlDataSource ID="XmlDataSource1" runat="server"

   [Sample  DataFile= "     COPY  TEXT BOX VALUEAND PASTE HERE;"  ]
            DataFile= "http://rss.news.yahoo.com/rss/topstories;"
            XPath="rss/channel/item" ontransforming="XmlDataSource1_Transforming1"></asp:XmlDataSource>

This article is working properly, but machine to machine you have to check whwther ISS is running or not. In my system ISS is running. First run your ISS, then it will work.

Up Next
    Ebook Download
    View all
    Learn
    View all