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
| 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.
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
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.
| 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>"); } |
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
The front end Default.aspx page looks something like this:| <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> |
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
The flow for the code behind page as follows.| 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>"); } } |
No comments:
Post a Comment