Friday, August 06, 2010

Consuming RSS Feeds in your ASP.NET website

Yesterday my friend suggested me to publish Feeds of my Blog on my company website, and I too liked this idea, so decided to update the website. now question is how to consume those RSS feeds in ASP.NET Page ?
There are various ways to use this, you can search for some standard third-party controls, or you can use JavaScript for this, but I think the simplest way is use XMLDataSource, because if you have checked the format of RSS feed then you must be knowing that RSS is nothing but XML file. and once you created this XMLDataSource then you can easily bind this with standard ASP.NET controls like DataList, Labels, Hyperlink etc. so lets see how we can do this in simple 4-5 steps.

  • Open Visual Studio and Create a Website or a web application Project.
  • on Default.aspx page in design area just drop a XMLDataSource control from Toolbox. Capture1
  • Click the smart tag and click on the link ‘Configure Data Source’. you can also right click on XMLDataSource and then click on option ‘Configure Data Source’.
  • This will launch the ‘Configure Data Source’ dialog box.
  • Add your feed address URL as Data file. e.g. http://feeds.cognitioninfotech.com/blogs/MyCoffeeCup
  • If you have transform file available ( .xsl) file then specify that file, else leave the field blank.
  • Enter rss/channel/item as XPath Expression.
 Capture2

  • Now I am going to add Repeater control since I just want to display Title of my post and a link to it. so add Repeater control and then click on smartTag to select DataSource.
  • From dropdown select above saved XMLDataSource1 as data source for Repeater control.
Capture3
  • After this I am going to switch to code and add <itemtemplate> in Repeater control, and add a Hyperlink control in it so that I can display title and link to my post. code will look like this…
    <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="http://feeds.cognitioninfotech.com/blogs/MyCoffeeCup" XPath="rss/channel/item"></asp:XmlDataSource> <asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1"> <ItemTemplate> <div> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl = '<%#XPath("link")%>' Text = '<%#XPath("title")%>' Target="_blank"> </asp:HyperLink> </div> <hr /> </ItemTemplate> </asp:Repeater>




you are done with core functionality coding, now if you want then you can decorate, using CSS styles, else simply press F5 to view in Browser…


Capture4

No comments: