Wednesday, August 25, 2010

Accessing Rss Feeds

RSS is a format for an XML document that describes a list of items, such as blog entries or news headlines. The document may just be a stream of XML created on the fly rather than a static file. In this post, we’ll tell you to put an RSS document (using Version 2.0) on a page by using the XmlDataSource and DataList controls.

Well what you need for this are as given below

  • URL to a valid rss file it could be .xml,.rss,.aspx.,.php etc
  • kind of schema of that rss file we call Xpath, to reach upto the content you are interested in.

A typical Rss file is like this.

A typical schema is like this

<rss>
<channel>
<item>
<title></title>
<description></description>
<link></link>
<pubDate></pubDate>
<!--...-->
</item>
</channel>
</rss>

XPath rss/channel/Item tell that this is Item from where you want to start fetching from.

Now i guess enough theoretical content has been described.

let’s start working.

Create a web site,

in default page or wherever you want to display the feeds put xmldatasource.

in navigationurl put the url of the rsss file. one example is (http://www.spaces.msn.com/members/mauliksoni/feed.rss). if you are choosing this Rss then put XPath=”rss/channel/item”.

take a data list and set its DataSourceID to the id of XmlDataSource you have taken.

in datalist set enableViewState to false.

Item template would be like this.

<ItemTemplate>
              Title:<%# XPath("title") %><br /><br />
              Description:<%# XPath("description")%>
              <hr />
           </ItemTemplate>

 

XPath(“Tiitle”) will print content of the Title tag and Xpath(“description”) will print content inside Description tag.

complete markup code is like this

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server"
            DataFile="http://www.spaces.msn.com/members/mauliksoni/feed.rss"
            XPath="rss/channel/item"></asp:XmlDataSource>
        <asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1" EnableViewState="False">
            <ItemTemplate>
               Title:<%# XPath("title") %><br /><br />
               Description:<%# XPath("description")%>
               <hr />
            </ItemTemplate>
        </asp:DataList>
    </div>
    </form>
</body>
</html>

Run the project and see the feeds from your favorite site. Enjoy.