Annsa - Useful Code Resources

Consuming an RSS Feed with ASP.NET and XSLT

RSS is a web content syndication format defined using XML. It is used by many sites and blogs to allow their content to be presented and aggregated. For example, the list on the right of this page is produced from an RSS feed from BBC News. You can find out more about RSS here.

This example shows how an external RSS feed can be rendered on a web page (like the one shown here). The example uses a custom ASP.NET control (written in C#) to encapsulate the functionality and simplify its inclusion on any host page. The sample server control uses public properties to allow it to be easily customised by the host page. The control also uses ASP.NET page caching features to limit the requests made by the host page to the remote RSS feed.

View a simple sample page here and its source code here.

The host page needs to include a Register page directive to allow the control to be referenced.

<@% Register TagPrefix="RSSFeed" TagName="ArticleList" Src="rssfeed.ascx" %>

The host page then refers to the control using the TagPrefix and TagName. This reference can also include HTML style attribute settings. These attributes map onto the public properties of the control. In this case they allow the host page to define the RSS feed, the maximum number of items to display and the XSLT to be used to transform the RSS content.

<RSSFeed:ArticleList RSSFeedURL="http://sampleDomain/rssfeed.xml" MaxDisplayItems="10" XSLFile="localSampleTransform.xsl" runat="server" />

The code for the server control can be seen here

The control uses the OutputCache page directive. This means that the control will only request a copy of the RSS field at known intervals. This is important since some RSS Feeds will impose a quota on requests from the same source.

The XSLT uses "local-name()" to avoid namespaces prefix problems and allow a single XSLT to process the widest range of RSS versions. The XSLT also includes CSS Style names in the output HTML (using the Class attribute) to allow the look and feel of the content to be controlled. If a more elaborate style was required, this could be embedded in the XSLT.

A sample XSLT file can be seen here

Alternatives? An ASP.NET DataList control could have been used to render the RSS, with the data loaded from the original XML then into a DataTable. The DataList control could then be bound to the DataTable. This approach avoids the need for an XSLT, but it might be harder to support all the flavours of RSS.

BBC Logo
Resolution on Syria vetoed at UN
Russia and China veto an Arab and Western-backed UN resolution condemning the violent crackdown in Syria, hours after scores are killed in Homs.
Nevada holds Republican vote
Republicans in the US state of Nevada take part in caucuses to decide their choice of presidential candidate with Mitt Romney leading the field.
Fidel Castro launches his memoirs
Former Cuban President Fidel Castro appears in public for the first time since April 2011 to launch a two-volume book of memoirs.
Afghan civilian death toll rises
The number of civilians killed and injured in the Afghan conflict has risen for the fifth year in a row, a UN report reveals.
Mexico 'cartel enforcer' detained
Police in Mexico say they have arrested the suspected leader of the Gente Nueva gang, the armed wing of the Sinaloa drug cartel.
Europe 'at risk of early grave'
Australia's Foreign Minister Kevin Rudd warns Europe faces an "early grave" if it continues to ignore Asia's rise.
ANC youth leader appeal dismissed
South African youth leader Julius Malema loses his appeals against the ruling ANC's decision to suspend him for bringing the party into disrepute.
Thousands in rival Moscow marches
Tens of thousands of people march in Moscow in protest at Prime Minister Vladimir Putin, while his supporters hold a rally elsewhere in Russia's capital.
Europe freeze hits transport hubs
Freezing weather hits transport hubs across Europe, as a cold spell that has now claimed 260 lives continues.
Monkeys fed wine 'to fight flu'
A zoo in Kazakhstan, where overnight temperatures have dipped to nearly -40C, is giving monkeys a wine concoction as a remedy against flu.
Sat, 04 Feb 2012 23:44:53 GMT