Showing posts with label kingslin. Show all posts
Showing posts with label kingslin. Show all posts

Tuesday, April 20, 2010

RSS Reader using ASP.NET

RSS Feed is very popular in Internet. This tutorial will show you how to create a RSS Reader using ASP.NET 2.0 and C#.

At first, import the namespace of System.Net, System.IO, and System.Xml

In this sample, we created a simple function to process the RSS feed from a sample URL. This function define a string of rssURL as its parameter. This string contains the RSS's URL. It will use the value of rssURL to create a WebRequest.

WebRequest is the abstract base class for the .NET Framework's request/response model for accessing data from the Internet. An application that uses the request/response model can request data from the Internet in a protocol-agnostic manner, in which the application works with instances of the WebRequest class while protocol-specific descendant classes carry out the details of the request.

Requests are sent from an application to a particular URI, such as a Web page on a server. The URI determines the proper descendant class to create from a list of WebRequest descendants registered for the application. WebRequest descendants are typically registered to handle a specific protocol, such as HTTP or FTP, but can be registered to handle a request to a specific server or path on a server.

The response to this request will be put into WebResponse object. The WebResponse class is the abstract base class from which protocol-specific response classes are derived. Applications can participate in request and response transactions in a protocol-agnostic manner using instances of the WebResponse class while protocol-specific classes derived from WebResponse carry out the details of the request. Client applications do not create WebResponse objects directly; they are created by calling the GetResponse method on a WebRequest instance.

After then, the WebResponse object will be used to create a stream to get the XML data. Stream is the abstract base class of all streams. A stream is an abstraction of a sequence of bytes, such as a file, an input/output device, an inter-process communication pipe, or a TCP/IP socket. The Stream class and its derived classes provide a generic view of these different types of input and output, isolating the programmer from the specific details of the operating system and the underlying devices. The we used a XmlDocument to store the stream data. XmlDocument manipulating the data of XML, finally read the RSS contents from Feed.






using System;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//RssDataSource1.Url = "http://blogs.sivababa.org/index.php/category/testimonials/feed";
//Eval("Title").ToString().Length>50 ? Eval("Title").ToString().Substring(0,49)+"..." : Eval("Title").ToString()

lblDate.Text = DateTime.Now.ToString();
}


protected void btnsmt_Click(object sender, EventArgs e)
{
if(txtrss.Text=="")
{
lblErrMsg.Text = "Please Rss URL";
return;
}
try
{
object Manifest = Cache.Get("Manifest");
if (Manifest != null)
{
//DataTable dt=new DataTable();
//dt =
Datalistmanifest.DataSource = Cache.Get("Manifest");
Datalistmanifest.DataBind();

}
else
{

RssDataSourceManifest.Url = txtrss.Text.Trim();
RssDataSourceManifest.MaxItems = 5;
DataTable dt = new DataTable();
Datalistmanifest.DataSourceID = "RssDataSourceManifest";
Datalistmanifest.DataBind();
Cache.Insert("Manifest", RssDataSourceManifest, null, DateTime.Now.AddHours(3), TimeSpan.Zero);
}
lblErrMsg.Text = "";
}
catch (Exception)
{
lblErrMsg.Text = "RSS Format is not correct";
}

}
protected void lbtncacheRemove_Click(object sender, EventArgs e)
{
Cache.Remove("Manifest");
}
}


Here I have used the cache to display RSS

Add RssToolkit dll in Bin folder