Consuming and Displaying an RSS 2.0 Feed Using the XML Control
Introduction
In this article, we will demonstrate how easy it is to consume and display an RSS
2.0 feed with ASP.NET. Specifically, we will use the ASP.NET XML web server control
to transform an RSS feed into a presentable format on a web page. To improve performance,
we will cache the page output for a defined period of time.
The RSS 2.0 XML file
Before we start coding, let's take a look at the structure of an RSS 2.0 file.
<rss version="2.0">
<channel>
<title>Channel title</title>
<link>Link to channel page</link>
<item>
<title>First content item</title>
<link>Link to first content item</link>
<pubDate>First content item publication date</pubDate>
</item>
<item>
<title>Second content item</title>
<link>Link to second content item</link>
<pubDate>Second content item publication date</pubDate>
</item>
<item>
<title>nth content item</title>
<link>Link to nth content item</link>
<pubDate>nth content item publication date</pubDate>
</item>
</channel>
</rss>
Note that RSS 2.0 includes a few other XML elements but the figure above illustrates
some of the more common ones.
Setting up the XML control
The first thing we need to do is to place an XML web server control on the web form.
For this example, we assign xmlRss to the ID attribute of the XML
control.
<asp:xml id="xmlRss" runat="server" />
Define the presentation template
We also need to create an XSL transform file which defines a presentation template
for the RSS feed. Here's a sample:
rss.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="rss/channel" />
</xsl:template>
<xsl:template match="channel">
<h2>
<a href="{link}" target="_blank"><xsl:value-of select="title" /></a>
</h2>
<ul>
<xsl:apply-templates select="item" />
</ul>
</xsl:template>
<xsl:template match="item">
<li>
<a href="{link}" target="_blank"><xsl:value-of select="title" /></a>
- <xsl:value-of select="pubDate" />
<br />
<xsl:value-of disable-output-escaping="yes" select="description" />
<p/>
</li>
</xsl:template>
</xsl:stylesheet>
Some RSS feeds contain escaped HTML formatting tags in the content. To avoid displaying
the tags themselves in the browser output, we set the disable-output-escaping attribute
of the <xsl:value-of ... /> element to "yes".
<xsl:value-of disable-output-escaping="yes" select="description" />
However, if you are just displaying headlines, then you may omit the above line.
Retrieving and displaying the XML content
We are now ready to write code to retrieve the remote RSS XML content. In order to
do this, we create an XmlDocument object to load the RSS feed. Note
that the XmlDocument object belongs to the System.Xml namespace.
The Load property of the XmlDocument object reads
the remote XML content into the object.
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load("<URL to RSS feed>");
The XmlDocument object can now be passed to the Document property
of the XML control. Next, we specify the filename of the XSL transform document in
the TransformSource property of the XML control.
xmlRss.Document = doc;
xmlRss.TransformSource = "rss.xsl";
Caching the output
If you are displaying an RSS feed on a website, it may not be a good idea to retrieve
the feed on every page load. By caching the content, we reduce the number of trips
needed to retrieve the RSS feed. The bottom line of caching: improved user experience,
and lighter load on the serving website as well as the website syndicating its content.
To cache the page output for one hour (3600 seconds), add the following line at the
top of the aspx page.
<%@ OutputCache Duration="3600" VaryByParam="none" %>
Demo
View demo (CNET News.com feed)
Source code
<%@ Page Language="C#" %>
<%@ OutputCache Duration="3600" VaryByParam="none" %>
<script runat="server">
private void Page_Load(object sender, EventArgs e)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load("http://news.com.com/2547-1_3-0-5.xml");
xmlRss.Document = doc;
xmlRss.TransformSource = "rss.xsl";
}
</script>
<html>
<body>
<form runat="server">
<asp:xml id="xmlRss" runat="server" />
</form>
</body>
</html>Things you can do
-
In keeping things simple, we have shown you a barebones XSL transform file. Modify
this file to suit your page formatting needs.
-
Encapsulate the code in a user control or custom server control and expose public
properties like the URL to the RSS file. In this way, we can reuse the control for
different RSS feeds.
-
Instead of caching the transformed output, use data caching to cache the RSS XML content.
-
Employ try...catch exception handling when loading the RSS feed as
the remote RSS feed may not be available at all times.
-
In some hosting situations, you may need to modify the code to access the RSS feed
via a proxy.
Conclusion
The article showcases the use of the ASP.NET XML web server control to consume a remote
RSS feed and transform the raw feed into formatted output using an XSL transform document.
Additionally, we also saw how we can improve web server performance and user experience
by caching the transformed RSS output.
Related links