Use ASP. NET to create an online RSS news aggregator (1)

Source: Internet
Author: User
Summary

This article explains how to use the XML Web control to obtain remote XML data, display the XML data on the ASP. NET page, and use the Repeater control to publish XML data in the database. In the past few years, with the increasing demand for data sharing between heterogeneous platforms, the use of XML has also exploded. Aware of this trend, Microsoft provides robust support for XML in the entire. NET Framework. This means that it is never so easy for ASP. NET developers to display and process XML data on Web pages. This article will learn XML and ASP. NET technologies by generating an rss2.0 aggregation engine and online news aggregator. This document assumes that you are familiar with ASP. NET and XML.
Introduction

With the increase of online time for office and home Internet access, as well as web sites and accessible Internet applicationsProgramWith the explosive growth, data sharing among applications becomes increasingly important. Sharing data between heterogeneous platforms requires a platform-neutral data format, which requires easy transmission through standard Internet protocols, which is exactly where XML is used. Because an XML file is essentially a text file, its encoding format is well known, and the existing XML Parser can be used for all mainstreamProgramming LanguageTherefore, XML data can be easily used by any platform.

Web website aggregation is an example of using XML to share data. It is often seen in news sites and blogs. Using WEB Website aggregation technology, websites can publish the latest content in XML format with accessible web aggregate files. There are many types of aggregation formats used by websites. The most popular one is rss2.0. (The rss2.0 specification is published on the technical topic of the Harvard Law website ). In addition, msdn magazine has an aggregation file: msdn magazine: this publication, which lists the latest issue of the msdn magazineArticleIncluding links to online version articles.

Once a web site has published an aggregate file, different clients can consume it. There are many ways to consume aggregate files. For example, a website that provides. Net technical resources may want to add the latest msdn magazine article title to the website. Aggregated files are often used by the news aggregator program, which is specially designed to obtain and display aggregated files from different sources.

As people pay more and more attention to XML data, the ability to process XML data on ASP. NET pages becomes more critical than ever before. Since Web site aggregation is so important, this article will create a web site aggregation file generator and an online news aggregator. When creating these two micro programs, we will show you how to access and display XML data, whether from a remote web server or a local file system. We will demonstrate how to display XML data in different ways, such as using the Repeater control and ASP. net xml Web Control.

Aggregate content using rss2.0 specifications

The first micro program we will create in this article is an aggregate file generator. For this mini-program, assume that you are a Web Developer of a large news website (such as msnbc.com), and all the news content is stored in the Microsoft SQL Server 2000 database. Specifically, these articles are stored in a table named articles. The following fields in the table are closely related to our program:

· ArticleID-primary key, an auto-increment integer field, used to uniquely identify each article;

· Title-specifies the title. Field Data Type: varchar (50 );

· Author-specify the author. Field Data Type: varchar (50 );

· Description-news content description, field data type: varchar (2000 );

· Datepublished-Date of news publication, field data type: datetime

Note that there may be other fields in the articles table. The above lists only the fields we need to use when creating an aggregate file. Moreover, this is just a very simple data model. In the application database environment, you may use a more standardized database model, such as having a separate authors table, there is a table that establishes many-to-many relationships between the author and the article.

Next, we will create an ASP. NET page and display the latest news list with the formatted rss2.0 XML file. Before describing how to complete this type of conversion on the ASP. NET page, we should first introduce the content of rss2.0 specifications. We should remember that in the entire specification, RSS is designed to provide a data model for aggregated content. There is no doubt that it has a series of XML elements used to describe the content to be aggregated by the web site, and a series of XML elements used to describe a specific news item. Finally, do not forget that the RSS aggregate file is an XML format file and must comply with the XML format rules, that is:

· All XML elements must be correctly nested;

· All attribute values must be enclosed in quotation marks;

· <,>, &, "And'' should be replaced with <,>, &, "And & apos ;;

Moreover, the XML format is case-sensitive, which means that the start and end tags of XML elements must match, and the spelling and Case sensitivity must be consistent.

The root element of rss2.0 is the <RSS> element, which can have a version number attribute, for example:

& Lt; RSS version = "2.0" & gt;
...
</RSS>

The <RSS> element has only one sub-element <channel> to describe the aggregated content. The <channel> element contains three required sub-elements to describe the information of the web site. The three elements are:

· Title-defines the name of the aggregate file. Generally, it also includes the name of the web site;

· Link-web site URL;

· Description-a brief description of the web site.

In addition, there are some optional elements to describe the site information. For more information about these elements, see the rss2.0 specification.

Each news item is placed in a separate <item> element. <Channel> An element can have any number of <item> elements. Each <item> element can have multiple sub-elements. The only requirement is that it must contain at least one of the <title> and <description> elements as sub-elements. The following lists some related <item> child elements:

· Title-news project title;

· Link-URL of a news project;

· Description-Outline of News projects;

· Author-author of the news project;

· Pubdate-news project release date

Below is a very simple rss2.0 aggregate file. You can see examples of other rss2.0 files from RSS generated by Radio userland.

& Lt; RSS version = "2.0" & gt;
<Channel>
<Title> latest datawebcontrols.com FAQs </title>
<Link> http://datawebcontrols.com </link>
<Description>
This is the syndication feed for the FAQs
At datawebcontrols.com
</Description>
<Item>
<Title> working with the DataGrid </title>
<Link> http://datawebcontrols.com/faqs/DataGrid.aspx </link>
<Pubdate> Mon, 07 Jul 2003 21:00:00 GMT </pubdate>
</Item>
<Item>
<Title> working with the repeater </title>
<Description>
This article examines how to work with the repeater
Control.
</Description>
<Link> http://datawebcontrols.com/faqs/Repeater.aspx </link>
<Pubdate> Tue 08 Jul 2003 12:00:00 GMT </pubdate>
</Item>
</Channel>
</RSS>

The <pubdate> element format is particularly important. RSS requires that the date be formatted in accordance with rfc822 Date and Time specifications. This format requires that a comma (,) be added to the beginning of an optional three-letter day-of-week abbreviation, the date must be followed by a three-character month and a year, followed by a time with a time zone name. In addition, note that the <description> sub-element is optional: the first news in the previous file does not have the <description> element, and the second news has one.

Output aggregated content through ASP. NET page

Now we know how to store our news items according to rss2.0 specifications. We are ready to create an ASP. NET page. When a user sends a request, it will return the aggregated content of the website. More specifically, we will create an ASP. NET page named RSS. aspx, which will return the latest five news items in the articles database table in the format specified by rss2.0.

There are several ways to accomplish this, which will be discussed later. But now, we need to do one thing first, that is, we need to get the latest five news items from the database. This can be obtained using the following SQL query statement:

Select top 5 ArticleID, title, author, description, datepublished from articles order by datepublished DESC
After obtaining the information, we need to convert the information into the corresponding rss2.0 format aggregate file. The easiest and quickest way to display database data as XML data is to use the Repeater control. To be precise, the repeater control displays <RSS> elements, <channel> elements, and site-related element tags in headertemplate and footertemplate templates, and <item> elements in itemtemplate templates. The following is the HTML section of the ASP. NET page (. aspx file:

<% @ page Language = "C #" contenttype = "text/XML" codebehind = "RSS. aspx. CS "
autoeventwireup =" false "inherits =" syndicationdemo. RSS "%>




Asp. net News!
http://www.ASPNETNews.com/Headlines/

This is the syndication feed for aspnetnews.com.



<% # formatforxml (databinder. eval (container. dataitem, <br> "title") %>

<% # formatforxml (databinder. eval (container. dataitem,
"Description") %>


http://www.ASPNETNews.com/Story.aspx? Id = <% #
databinder. eval (container. dataitem, "ArticleID") %>

<% # formatforxml (databinder. eval (container. dataitem,
"author") %>

<% # string. format ("{0: R}",
databinder. eval (container. dataitem,
"datepublished ")) %>


<Footertemplate>
</Channel>
</RSS>
</Footertemplate>
</ASP: repeater>

The first thing to note is: the above sectionCodeThe example only contains the Repeater control, without other HTML tags or Web controls. This is because we want the page to only output data in XML format. In fact, observe the @ page command and you will find that contenttype is set to the xml mime type (text/XML ). Note that when the database fields title, description, and author are added to the XML output in the itemtemplate, the auxiliary function formatforxml () is called (). We will soon see that this function is defined in the background encoding class. Its function is to replace invalid xml characters with their corresponding legal escape characters. Finally, we should note that the database Field datepublished in the <pubdate> element is formatted using string. format. The standard format descriptor "R" is used to format the value of datepublished.

The background encoding code of this web page is not complex. The page_load event processing function only binds the database query results to the Repeater control. The formatforxml () function performs some simple string replacement as needed. For simplicity, the following example only lists the code for these two functions:

See Figure 1 for accessing the RSS. ASPX page in a browser.

Figure 1 accessing the RSS. ASPX page through a browser

before we generate an online news aggregator, let me talk about some possible enhancements of this aggregation engine. First, each time you access the RSS. ASPX page, you must access the database. If a large number of people are expected to frequently access the RSS. ASPX page, it is very valuable to use the output cache. Secondly, news websites generally classify aggregated content into different categories. For example, News.com has some specialized aggregation content areas, such as for enterprise computing, e-commerce, and communication content. Adding the Category field indicating the category in the database table articles can easily provide this support. In this way, on the RSS. ASPX page, you can receive a query parameter indicating the Display category, and then only search for the specified news item category.

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.