10
RSS Feed using ASP.NET & Linq http://www.livetolearn.in

Creating an RSS feed

Embed Size (px)

DESCRIPTION

Creating and displaying RSS feed by using LINQ asp.netbyhttp://www.livetolearn.in

Citation preview

Page 1: Creating an RSS feed

RSS Feed using ASP.NET & Linq

http://www.livetolearn.in

Page 2: Creating an RSS feed

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.

http://www.livetolearn.in

Page 3: Creating an RSS feed

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

http://www.livetolearn.in

Page 4: Creating an RSS feed

Add a generic handler named rsshandler.ashx

Add import directive Imports System.Xml.Linq

Replace the process request() subroutine as follows

http://www.livetolearn.in

Page 5: Creating an RSS feed

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

Dim rssdoc As New XDocument(New XDeclaration("1.0", Nothing, Nothing))

rssdoc.Add(New XComment("My RSS Feed uses XML to Linq datasource"))

Dim rssrootelement As New XElement("rss", New XAttribute("version", "2.0"))

Dim rsschannel As New XElement("channel") Dim rsstitle As New XElement("title", "RK's News Channel")

rsschannel.Add(rsstitle) Dim rssdesc As New XElement("description", "Description of Channel")

rsschannel.Add(rssdesc) Dim rsslink As New XElement("link", "http://www.livetolearn.in/")

rsschannel.Add(rsslink) Dim intCtr As Integer Dim rssitem As XElement

RSS Document CommentRSS Document Comment

RSS Channel Title (Appears at title bar)RSS Channel Title (Appears at title bar)

RSS Title LinkRSS Title Link

http://www.livetolearn.in

Page 6: Creating an RSS feed

For intCtr = 0 To 10 rssitem = New XElement("item", _ New XElement("title", "This is item number " & intCtr.ToString), _

New XElement("description", "Description for item # " & _

intCtr.ToString), _ New XElement("link", "http://www.livetolearn.in/item" & _

intCtr.ToString & ".aspx")) rsschannel.Add(rssitem) Next rssrootelement.Add(rsschannel) rssdoc.Add(rssrootelement) rssdoc.Save((New System.IO.StreamWriter _

(context.Response.OutputStream))) End Sub

This for loop creates 10 sample items with Description.

This for loop creates 10 sample items with Description.

http://www.livetolearn.in

Page 7: Creating an RSS feed

Now, browse the rsshandler.ashx with Internet Explorer 7 and above or Firefox

http://www.livetolearn.in

Page 8: Creating an RSS feed

Displaying XML data

http://www.livetolearn.in

Page 9: Creating an RSS feed

Add an XmlDataSource control to a ASP.NET page

Click smart tag Choose configure data source Enter the URL of the RSS feedFor examplehttp://www.livetolearn.in/blog/?feed=rss2 Type the following in Xpath expression text boxRss/channel/item Add a data list control and set source to

Xmldatasource1, Edit Item template of data list control Add a hyper link control inside the item template Set it’s data binding property as follows

Navigate URL – xPath(“link”) xPath(“title”) It displays the list of items from rss feed.

http://www.livetolearn.in

Page 10: Creating an RSS feed

By adding more items in Item Template (e.g. Text box), we can display description from RSS feed.

********

http://www.livetolearn.in