Creating the Syndication Output via an ASP.NET Web Page
Now that we've seen how our news items are stored along with the RSS 2.0 specification, we're ready to create an ASP.NET Web page that, when requested, will return our Web site's syndicated content. More specifically, we'll create an ASP.NET Web page named rss.aspx that will return the five most recent news items from the Articles database table, formatted according to the RSS 2.0 specification.
There are a number of ways to accomplish this, which we'll examine shortly. First things first, though: we need to get the five most recent news items from the database. This can be done with the following SQL query:
SELECT TOP 5 ArticleID, Title, Author, Description, DatePublished
FROM Articles
ORDER BY DatePublished DESC
Once we have this information, we need to render it into an appropriate RSS 2.0 syndication file. The simplest and quickest way to display the database data as XML data is to use a Repeater control. Specifically, the Repeater will display the <rss>, <channel>, and Web site related elements tags in its HeaderTemplate and FooterTemplate templates, and the <item> elements in the ItemTemplate template. The following is the HTML portion of our ASP.NET Web page (the .aspx file):
<%@ Page language="c#" ContentType="text/xml" Codebehind="rss.aspx.cs"
  AutoEventWireup="false" Inherits="SyndicationDemo.rss" %>
<asp:Repeater id="rptRSS" runat="server">
  <HeaderTemplate>
   <rss version="2.0">
    <channel>
    <title>ASP.NET News!</title>
    <link>http://www.ASPNETNews.com/Headlines/</link>
   <description>
    This is the syndication feed for ASPNETNews.com.
   </description>
  </HeaderTemplate>
  <ItemTemplate>
    <item>
    <title><%# FormatForXML(DataBinder.Eval(Container.DataItem,
     "Title")) %></title>
    <description>
     <%# FormatForXML(DataBinder.Eval(Container.DataItem,
"Description")) %>
    </description>
    <link>
     http://www.ASPNETNews.com/Story.aspx?ID=<%#
     DataBinder.Eval(Container.DataItem, "ArticleID") %>
    </link>
    <author><%# FormatForXML(DataBinder.Eval(Container.DataItem,
     "Author")) %></author>
    <pubDate>
    <%# String.Format("{0:R}",
     DataBinder.Eval(Container.DataItem,
      "DatePublished")) %>
    </pubDate>
    </item>
  </ItemTemplate>
  <FooterTemplate>
   </channel>
    </rss>
  </FooterTemplate>
</asp:Repeater>
The first thing to note is that the above code example contains only the Repeater control and no other HTML markup or Web controls. This is because we want this page to emit nothing but XML output. In fact, if you look in the @Page directive you will find that the ContentType has been set to the XML MIME type (text/xml). Next, in the ItemTemplate when adding the Title, Description and Author database fields to the XML output, we call the helper function FormatForXML(). This function is defined in the code-behind class-which we'll examine shortly-and simply replaces any illegal XML characters with their escaped, legal equivalent. Finally, the DatePublished database field, entered into the element, is formatted appropriately using the String.Format() method. The standard format specifier R formats the DatePublished value appropriately.
The code-behind class for this Web page is rather straightforward. The Page_Load event handler simply binds the database query to the Repeater, while the FormatForXML() function does a few simple string replacements, if needed. For brevity, only the Page_Load event handler and FormatForXML() function are shown from the code-behind class:
private void Page_Load(object sender, System.EventArgs e)
{
  // Connect to the Database
  SqlConnection myConnection = new SqlConnection(connection string);
  // Retrieve the SQL query results and bind it to the Repeater
  string SQL_QUERY = "SELECT TOP 5 ArticleID, Title, Author, " +
       "Description, DatePublished " +
       "FROM Articles ORDER BY DatePublished DESC";
  SqlCommand myCommand = new SqlCommand(SQL_QUERY, myConnection);
  // bind the results to the Repeater
  myConnection.Open();
  rptRSS.DataSource = myCommand.ExecuteReader();
  rptRSS.DataBind();
  myConnection.Close();
}
protected string FormatForXML(object input)
{
  string data = input.ToString(); // cast the input to a string
  // replace those characters disallowed in XML documents
  data = data.Replace("&", "&");
  data = data.Replace("\"", """);
  data = data.Replace("'", "'");
  data = data.Replace("", ">");
  return data;
}
A screenshot of rss.aspx, when viewed through a browser, can be seen in Figure 1.
Figure 1. Rss.aspx, When Viewed Through a Browser
Before moving on to building an online news aggregator, let me mention some possible enhancements for the syndication engine. First, every time the rss.aspx Web page is requested, a database access occurs. If you expect a large number of people to be frequently accessing rss.aspx, it would be worthwhile to use output caching. Second, typically news sites break down their syndication into a number of categories. For example, News.com has specialized syndicated sections, such as content focusing on Enterprise Computing, E-Business, Communications and so on. Providing such support could be easily accomplished provided the Articles table has some sort of Category field. The rss.aspx Web page, then, could accept a querystring parameter indicating what category to display, and then retrieve only those news items for the specified category.
*This article originally appeared on the ASP.NET Dev Center at MSDN
Scott Mitchell, author of five ASP/ASP.NET books and founder of 4GuysFromRolla.com, has been working with Microsoft Web technologies for the past five years. An active member in the ASP and ASP.NET community, Scott is passionate about ASP and ASP.NET and enjoys helping others learn more about these exciting technologies. For more on the DataGrid, DataList, and Repeater controls, check out Scott's book ASP.NET Data Web Controls Kick Start (ISBN: 0672325012). Read his blog at : http://scottonwriting.net
Creating an Online RSS News Aggregator with ASP.NET Part 2
0 views
Comments (0)
Please sign in to leave a comment.
No comments yet. Be the first to comment!