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
Six men jailed for Portugal abuse
Six Portuguese men are sentenced to up to 18 years in jail after being found guilty of multiple charges of sexual abuse at a state-run children's home.
Pakistan rally bomb kills dozens
A bomb kills at least 50 people at a Shia Muslim rally in the south-western city of Quetta, the second attack on Pakistan's religious minority in days.
Earthquake hits south New Zealand
A 7.0-magnitude earthquake strikes New Zealand's South Island, causing widespread minor damage and power cuts.
US cargo plane crashes in Dubai
A UPS cargo plane crashes at an air force base shortly after take-off from Dubai airport, killing two crew members on board.
BP blowout preventer 'removed'
BP removes the blowout preventer that failed to stem the leaking Gulf of Mexico oil well and says it has paid $8bn (£5.2bn) in damage costs.
UN calls special food price talks
The United Nations' food agency calls a special meeting of policy makers to discuss the recent rapid rises in food prices.
Clinton warns on Mid-East talks
The US secretary of state warns the current round of Mid-East peace talks may be "the last chance for a very long time".
Mexican clash 'kills 27 gunmen'
Suspected drug gang members are killed near the US border, Mexico says, hours after President Calderon vows to continue fighting cartels.
Hurricane Earl skirts eastern US
A weakened hurricane Earl skirts past North Carolina's Outer Banks and continues up the US east coast as a "category one" storm.
US monthly job losses continue
The US economy shed another 54,000 jobs in August, the third month in a row that jobs have been lost, official figures show.
Fri, 03 Sep 2010 21:57:29 GMT