ASP.NET: How to Bind RSS Feed To Repeater
Repeater seems to be the easiest, light, most simple and generic control in ASP.NET to render repeated items. For example in data driven applications it is easy to render table contents (i.e. table rows) with Repeater. All developer needs to do is to bind a custom class collection, Array, List
However one can feel some lack of support in Repeater for RSS feeds in other words it is not easy to bind repeated to dynamic online XML structure like RSS.
Below I tried to demonstrate a small and simple example of how to bind RSS feed to ASP.NET Repeater control on the fly. It's implemented as a control which we can drop anywhere on ASP.NET page.
< %@ Control Language="c#" AutoEventWireup="true" EnableViewState="false" %> < %@ Import namespace="System.Xml" %> < script runat="server" language="C#"> public string rssUrl = "http://blogs.x2line.com/al/rss.aspx"; private System.Xml.XmlDocument doc; public override void DataBind() { doc = new System.Xml.XmlDocument(); doc.Load(rssUrl); base.DataBind(); } public void Page_Load(System.Object s, System.EventArgs e) { this.DataBind(); } < /script> < asp:Repeater runat="server" id="rptrRss" DataSource='< %# doc.SelectNodes("/rss/channel/item[position()<=5]") %>'> < HeaderTemplate> < div> < a href='< %# doc.SelectSingleNode("/rss/channel/image/link").InnerText %>'> < img src='< %# doc.SelectSingleNode("/rss/channel/image/url").InnerText %>' alt='< %# doc.SelectSingleNode("/rss/channel/image/title").InnerText %>' /> < /a> < /HeaderTemplate> < ItemTemplate> < a href='< %# (Container.DataItem as XmlNode)["link"].InnerText %>'> < %# (Container.DataItem as XmlNode)["title"].InnerText %> (< %# (Container.DataItem as XmlNode).SelectSingleNode("author | title[not(../author)]").InnerText %>) < /a> < /ItemTemplate> < FooterTemplate> < /div> < /FooterTemplate> < /asp:Repeater>
Maybe useful to make widgets. Enjoy :)
Saturday, June 21, 2008 2:18 PM