C # + ASP. NET Develop a web-based RSS reader

Source: Internet
Author: User
Document directory
  • C # + ASP. NET Develop a web-based RSS reader
C # + ASP. NET Develop a web-based RSS reader

Recently I have been searching for ways to display RSS feed on the web page. I chose C # and ASP. NET as tools. I created a simple processing function to process the RSS feed obtained from a URL. You can directly use this simple function or convert it into the desired function.
This function uses a string rssurl as its parameter. This string contains the URL of RSS. It creates a webrequest item using the value of rssurl:

System. net. webrequest myrequest = system. net. webrequest. Create (rssurl );

The response of this request will be put in a webresponse object:

System. net. webresponse myresponse = myrequest. getresponse ();

Then the webresponse object is used to create a stream to retrieve the XML value:

System. Io. Stream rssstream = myresponse. getresponsestream ();

You can then use an xmldocument object to store the XML content in the stream. The xmldocument object is used to call XML content:

System. xml. xmldocument rssdoc = new system. xml. xmldocument ();
Rssdoc. Load (rssstream );

Because RSS feed is not just an XML file, we can assume that it contains some RSS standards. Here, we assume that RSS 2.0 is used. You can get detailed information from http://blogs.law.harvard.edu/tech/rss.
Specifically, each item should be in RSS/channel. Use XPath to create an item node list as follows:

System. xml. xmlnodelist rssitems = rssdoc. selectnodes ("RSS/channel/item ");

Rssitems stores information about getting all item nodes from RSS. In this way, you can obtain the information needed internally. The title, link, and description of each item are displayed. Each item stored in rssitems, each Tag Element can be extracted using the selectsinglenode method. The returned value is assigned to an xmlnode object. The following code obtains a title node:

System. xml. xmlnode rssdetail;
Rssdetail = rssitems. Item (I). selectsinglenode ("title ");

Now the tag needs to be extracted, and innertext is used to complete this work. After selectsinglenode is called, you can use rssdetail to test whether the formatted rss xml contains some tags:

If (rssdetail! = NULL) {Title = rssdetail. innertext;} else {Title = "";}

In this way, you can get RSS content from a feed. The rest is to call this method to display the feed content. The following is a complete example of using ASP. NET:

<% @ Page Language = "C #" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<SCRIPT runat = "server">
Public void processrssitem (string rssurl)
{
System. net. webrequest myrequest = system. net. webrequest. Create (rssurl );
System. net. webresponse myresponse = myrequest. getresponse ();
System. Io. Stream rssstream = myresponse. getresponsestream ();
System. xml. xmldocument rssdoc = new system. xml. xmldocument ();
Rssdoc. Load (rssstream );
System. xml. xmlnodelist rssitems = rssdoc. selectnodes ("RSS/channel/item ");
String title = "";
String link = "";
String description = "";
For (INT I = 0; I <rssitems. Count; I ++)
{
System. xml. 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> ");
}
}
</SCRIPT>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<%
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 );
%>
</Div>
</Form>
</Body>
</Html>

The result is as follows:


Display result

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.