|
| Consuming RSS Feed using ASP.NET |
 |
Thu, 28 Feb 2008 17:58:37 +000 |
RSS stands for Really Simple Syndication. It is a format used to publish
regularly updated web content such as blogs, articles, news sites etc. Now if
you happen to visit a website regularly and have to often to go that website
physically, to check if there is some new content available, then RSS feed is
what you are looking out for. If that website provides RSS feed, all you need to
do is subscribe to it. You can then either use any RSS reader which checks for
updated content frequently and displays it to you or consume RSS feed in your
own application. Handy, isn’t it! For eg: I assume some of you like visiting
dotnetcurry.com to check for new articles. Instead what you can do is subscribe
to our RSS feed over here:http://feeds.feedburner.com/netCurryRecentArticlesThen
by using a RSS reader, you can view the latest updated content of this site.
Without further ado, let us see how to consume a RSS feed of any website in your
application. For demonstration purposes, we will be consuming the RSS feed of
dotnetcurry.com in our application.Note: Once you visit the url given above,
right click on the page and View Source. You will find an XML file, since RSS is
nothing but an XML file.Step 1: Create a new website (Open VS 2005 > File
> New Website) called ‘RSSFeed’.Step 2: Drag and drop a XMLDataSource
from the toolbox on to the Default.aspx. If the ‘Configure Data Source’
option is not visible on the XMLDataSource, click the smart tag (arrow just
above the XMLDataSource) to make it visible and click on the link.Step 3: The
‘Configure Data Source’ dialog appears. Add the following entries:Data File
- http://feeds.feedburner.com/netCurryRecentArticlesTransform File – Specify
an .XSL file if you have one. This is used to beautify or change the layout of
the XML file.XPath Expression - rss/channel/itemOnce the entries has been added,
click ok to close the dialog.Step 4: Now drag and drop a ‘DataList’ control
from the toolbox on to Default.aspx. Click the smart tag to display the
‘Choose Data Source’. Choose ‘XMLDataSource1’ option from the
dropdown.Step 5: Now go to the ‘Source’ view of Default.aspx. Create an
<ItemTemplate> within the <asp:DataList> element as shown below:
<ItemTemplate>
<%#XPath("title")%><br/>
<%#XPath("pubDate")%><br/>
<%#XPath("author")%><br/>
<%#XPath("description")%></ItemTemplate>After adding some
UI look and feel to the DataList, the entire source code will look similar to
the
following:C#<%@PageLanguage="C#"AutoEventWireup="true"
CodeFile="Default.aspx.cs"Inherits="_Default"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0
Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.
dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="se
rver"> <title>RSS Feed</title></head><body>
<formid="form1"runat="server"> <div>
<asp:XmlDataSourceID="XmlDataSource1"runat="server"DataFi
le="http://feeds.feedburner.com/netCurryRecentArticles"
XPath="rss/channel/item"></asp:XmlDataSource>
</div>
<asp:DataListID="DataList1"runat="server"DataSourceID=&qu
ot;XmlDataSource1"BackColor="White"BorderColor="#404040"
;BorderStyle="Solid"GridLines="Vertical">
<ItemTemplate>
<%#XPath("title")%><br/>
<%#XPath("pubDate")%><br/>
<%#XPath("author")%><br/>
<%#XPath("description")%> </ItemTemplate>
<AlternatingItemStyleBackColor="CadetBlue"/>
<ItemStyleBackColor="AliceBlue"ForeColor="Black"/>
<HeaderStyleBackColor="#804040"ForeColor="White"Font-Bold
="true"/> </asp:DataList>
</form></body></html>
VB.NET<%@PageLanguage="VB"AutoEventWireup="false"
CodeFile="Default.aspx.vb"Inherits="_Default"%>
<htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="se
rver"> <title>RSS Feed</title></head><body>
<formid="form1"runat="server"> <div>
<asp:XmlDataSourceID="XmlDataSource1"runat="server"DataFi
le="http://feeds.feedburner.com/netCurryRecentArticles"
XPath="rss/channel/item"></asp:XmlDataSource>
</div>
<asp:DataListID="DataList1"runat="server"DataSourceID=&qu
ot;XmlDataSource1"BackColor="White"BorderColor="#404040"
;BorderStyle="Solid"GridLines="Vertical">
<ItemTemplate>
<%#XPath("title")%><br/>
<%#XPath("pubDate")%><br/>
<%#XPath("author")%><br/>
<%#XPath("description")%> </ItemTemplate>
<AlternatingItemStyleBackColor="CadetBlue"/>
<ItemStyleBackColor="AliceBlue"ForeColor="Black"/>
<HeaderStyleBackColor="#804040"ForeColor="White"Font-Bold
="true"/> </asp:DataList>
</form></body></html> That’s it. Run the application. You
will see that the top 5 recently published articles are shown in your webpage.
You can verify that by going to the home page of www.dotnetcurry.com and viewing
the top 5 articles over there. You can use this code to consume RSS feed of
other websites as well. All you would need to do is specify the correct XPath
expression based on the XML structure of the RSS.I hope this article was useful
and I thank you for viewing it. If you liked the article, please subscribe to my
RSS feed over here.Original Article source :
http://www.dotnetcurry.com/ShowArticle.aspx?ID=116
|
| Post Reply
|
| Re: Consuming RSS Feed using ASP.NET |
 |
Mon, 17 Mar 2008 10:51:32 +000 |
Hi Mate
I have just been tryin this out but trying to access the BBC Weather info via
this DataFile attribute
"http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/0009.xml"
I keep gettin a 404 even though it displays fine in the browser.
2 things seem to be very difficult with it at the mionute.
1) When i view source on the BBC Weather rss feed page I get 3 lines of
code with no data in there.
2) I cant find out what the XPath element names are
Has anyone else had any luck with the BBC Weather feed? - I got the Curry one
working fine by the way.
Cheers
Dannster
|
| Post Reply
|
| Re: Consuming RSS Feed using ASP.NET |
 |
Mon, 17 Mar 2008 15:19:36 +000 |
Hi Talib
Thanks for your response. I don't understand what you mean by a
response.Redirect - I probably don't know the method as of yet. The ways I have
tried so far are these.
Trying to populate an XmlReader
Tryin to specify the DataFile in the XmlDataSource
<asp:XmlDataSource ID="XmlDataSource1" runat="server"
DataFile="http://feeds.bbc.co.uk/weather/feeds/rss/world/0009.xml"
TransformFile="http://feeds.bbc.co.uk/weather/feeds/rss/weather.xsl"&g
t;</asp:XmlDataSource>
<asp:DataList ID="DataList1" runat="server"
DataSourceID="XmlDataSource1">
</asp:DataList>
I wasn't too sure where a Response.Redirect would play a part.
Cheers
Dannster
|
| Post Reply
|
| Re: Consuming RSS Feed using ASP.NET |
 |
Mon, 17 Mar 2008 18:06:45 +000 |
Hi dannster76 ,
How are you accessing the BBC feed i.e ; by using Response.Redirect
or through any other means..
I tried it with Response.Redirect and it works fine ..
-----
TALIB ALI KHAN
Microosoft Certified Technology Specialist
|
| Post Reply
|
| Re: Consuming RSS Feed using ASP.NET |
 |
Thu, 20 Mar 2008 21:19:15 +000 |
When I do this the hyperlinks for each item in the datalist are not active. How
do I put the hyperlinks to each item into the list?
|
| Post Reply
|
|
|
|
|
|
|
|
|
|