RSS Reader using ASP.NET 2.0 and C# 2005
See more tutorials in XML. This post has Comments Off on RSS Reader using ASP.NET 2.0 and C# 2005.
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.
0 1 2 3 4 |
using System.Net using System.IO using 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.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
protected void Page_Load(object sender, EventArgs e) { string rssURL = "http://www.codeguru.com/icom_includes/feeds/codeguru/rss-all.xml"; Response.Write("<font size=5><b>Site: " + rssURL + "</b></font><Br />"); ProcessRSSItem(rssURL); Response.Write("<hr />"); rssURL = "http://www.developer.com/icom_includes/feeds/special/dev-5.xml"; Response.Write("<font size=5><b>Site: " + rssURL + "</b></font><Br />"); ProcessRSSItem(rssURL); } public void ProcessRSSItem(string rssURL) { WebRequest myRequest = WebRequest.Create(rssURL); WebResponse myResponse = myRequest.GetResponse(); Stream rssStream = myResponse.GetResponseStream(); XmlDocument rssDoc = new XmlDocument(); rssDoc.Load(rssStream); XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item"); string title = ""; string link = ""; string description = ""; for (int i = 0; i { XmlNode rssDetail; rssDetail = rssItems.Item(i).SelectSingleNode("title"); if (rssDetail != null) { title = rssDetail.InnerText; } else { title = ""; } rssDetail = rssItems.Item(i).SelectSingleNode("link"); if (rssDetail != null) { link = rssDetail.InnerText; } else { link = ""; } rssDetail = rssItems.Item(i).SelectSingleNode("description"); if (rssDetail != null) { description = rssDetail.InnerText; } else { description = ""; } Response.Write("<p><b><a href='" + link + "' target='new'>" + title + "</a></b><br/>"); Response.Write(description + "</p>"); } } |
The front end Default.aspx page looks something like this:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>RSS</title> </head> <body> <form id="form1" runat="server"> <div> <fieldset style="height: 383px"> <legend><strong>RSS</strong></legend><br /> </fieldset> </div> </form> </body> </html> |
The flow for the code behind page as follows.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Configuration; using System.Web.Security; using System.Net; using System.IO; using System.Xml; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string rssURL = "http://www.codeguru.com/icom_includes/feeds/codeguru/rss-all.xml"; Response.Write("<font size=5><b>Site: " + rssURL + "</b></font><Br />"); ProcessRSSItem(rssURL); Response.Write("<hr />"); rssURL = "http://www.developer.com/icom_includes/feeds/special/dev-5.xml"; Response.Write("<font size=5><b>Site: " + rssURL + "</b></font><Br />"); ProcessRSSItem(rssURL); } public void ProcessRSSItem(string rssURL) { WebRequest myRequest = WebRequest.Create(rssURL); WebResponse myResponse = myRequest.GetResponse(); Stream rssStream = myResponse.GetResponseStream(); XmlDocument rssDoc = new XmlDocument(); rssDoc.Load(rssStream); XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item"); string title = ""; string link = ""; string description = ""; for (int i = 0; i { XmlNode rssDetail; rssDetail = rssItems.Item(i).SelectSingleNode("title"); if (rssDetail != null) { title = rssDetail.InnerText; } else { title = ""; } rssDetail = rssItems.Item(i).SelectSingleNode("link"); if (rssDetail != null) { link = rssDetail.InnerText; } else { link = ""; } rssDetail = rssItems.Item(i).SelectSingleNode("description"); if (rssDetail != null) { description = rssDetail.InnerText; } else { description = ""; } Response.Write("<p><b><a href='" + link + "' target='new'>" + title + "</a></b><br/>"); Response.Write(description + "</p>"); } } } |