C#+asp. NET development of web-based RSS reader

Source: Internet
Author: User
Asp.net|rss|web recently I've been looking for ways to display RSS feeds on Web pages, and I've chosen C # and ASP.net as tools. I created a simple processing function to handle the RSS feed from a URL. You can use this simple function directly, or transform it into the function you want.

This function uses a string rssurl as its argument. This string contains the URL of the 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 placed in a WebResponse object:

System.Net.WebResponse myresponse = Myrequest.getresponse ();
The WebResponse object is then used to create a stream to take out the value of the XML:

System.IO.Stream Rssstream = Myresponse.getresponsestream ();
You can then use a XmlDocument object to store the XML content in the stream. The XmlDocument object is used to transfer content into the XML:

System.Xml.XmlDocument Rssdoc = new System.Xml.XmlDocument ();
Rssdoc.load (Rssstream);
Since the RSS feed is not just an XML file, we can assume that it contains some of the RSS Standard Rules. Here, we assume that RSS 2.0 is used. You can get the detailed content of the specification from the Http://blogs.law.harvard.edu/tech/rss.

Specifically, each item should be in the rss/channel/. Using XPath expressions, a list of item nodes can be created as follows:

System.Xml.XmlNodeList Rssitems = rssdoc.selectnodes ("Rss/channel/item");
Rssitems stores information from RSS for all item nodes. So you can get the information you need internally. Here, the title, link, and description of each item will be displayed. For each item stored in the Rssitems, each tag element can be extracted using the selectSingleNode method. The returned value is assigned to a XmlNode object. The following code obtains a header node:

System.Xml.XmlNode Rssdetail;
Rssdetail = Rssitems.item (i). selectSingleNode ("title");
Now the tags need to be extracted and used innertext to complete the work. After calling selectSingleNode, you can use Rssdetail to test whether the formatted RSS XML contains some markup:

if (rssdetail!= null) {title = Rssdetail.innertext;} else {title = ' ";}
In this way, you have completed the work of getting RSS content from a feed. The rest of the work is to call this method to display the contents of the feed. 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 = "";
}

[1] [2] Next page



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.