Community-Credit.com | NonProfitWays.com | SOAPitstop.com   Skin:   
      User: Not logged in 
Home
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Presentations
Featured Articles
Book Chapters
Training Courses
Events
NewsGroups
 
Discussions
Examples
Tutorials
Tools
Articles
Resources
Websites
 
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
Logout
 
About Me
My Blog
HeadGeek Articles
Talking Portfolio
Resume
Pictures
World Trip Pics

Read/Write an Atom and RSS example code
Author: Sonu Kapoor
Published: 12/9/2004 12:00 AM
Category: XML
Summary: In this article I will show you how easily you can read/write an Atom feed as well as an RSS feed.

 
If you don’t know how a RSS feed or an Atom feed looks like, make sure to read the article: Is ATOM really better than RSS? To generate these feeds I have created two classes, which will do the major work for us. All you will need is to call the methods provided by these two classes and your feeds are ready in minutes. The .NET framework provides some really nice classes to handle XML data. Since Atom and RSS feeds are simply XML files, I used these classes to form my own Atom and RSS class. Once you are done with the article, you will be easily able to create such feeds. Here is a small screenshot after reading the feeds.

Reading an Atom feed

The following snippet from the Atom class shows how you can read an Atom feed. It returns a dataset, which can be then easily used for a datagrid or any other control, which has the datasource property. All you only have to call is the ReadAtomFeed() function via the Atom object.

'' Create atom object
Dim objAtom As New Atom.AtomFeed
Private Sub Page_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
  SetAtomFeedToDataGrid()
End Sub
Private Function SetAtomFeedToDataGrid()
  '' set atom file
  objAtom.FilePath = ""atom.xml""
  '' Set new dataset
  Dim ds As New DataSet
  '' read the feed 
  ds = objAtom.ReadAtomFeed()
  '' set dataset to the datagrid
  DataGrid1.DataSource = ds
  '' bind datagrid
  DataGrid1.DataBind()
End Function

Writing an Atom Feed

Writing an Atom feed is very easy with the provided functions. You simply call these methods like shown in the following example.

'' Set filename of the atom to be created
objAtom.FilePath = ""atom.xml""
objAtom.WriteStartElement(""XMLPitStop"", _
  ""http://www.XMLPitStop.com"", _
  ""Largest Resource of XML examples on the Web"", _
  ""http://www.XMLPitStop.com"", _
  ""David Silverlight"", _
  ""2004-11-20T14:16:00-05:00"")
'' Create first atom feed entry
objAtom.CreateEntry(""Entry1"", _
  ""http://xmlpitstop.com/entry1"", _
  ""2004-11-20T14:16:00-05:00"", _
  ""2004-11-20T14:16:00-05:00"", _
  ""2004-11-20T14:16:00-05:00"", _
  ""Description of entry 1."")
'' Create second atom feed entry
objAtom.CreateEntry(""Entry2"", _
  ""http://xmlpitstop.com/entry2"", _
  ""2004-11-20T14:16:00-05:00"", _
  ""2004-11-20T14:16:00-05:00"", _
  ""2004-11-20T14:16:00-05:00"", _
  ""Description of entry 2."")
objAtom.WriteEndElement()

And voila you just created the following atom feed:

<?xml version=""1.0""?>
<feed version=""0.3"" xmlns:dc=""http://purl.org/dc/elements/1.1/""
xmlns:trackback=""http://madskills.com/public/xml/rss/module/trackback/""
xmlns:wfw=""http://wellformedweb.org/CommentAPI/""
xmlns:slash=""http://purl.org/rss/1.0/modules/slash/""
xmlns=""http://purl.org/atom/ns#"" xml:lang=""en-US"">
<title>XMLPitStop</title>
  <link rel=""alternate"" type=""text/html"" href=""http://www.XMLPitStop.com"" />
  <tagline type=""text/html"">Largest Resource of XML examples on the Web</tagline>
  <id>http://www.XMLPitStop.com</id>
  <author>
    <name>David Silverlight</name>
    <url>http://www.XMLPitStop.com</url>
  </author>
  <modified>2004-11-20T14:16:00-05:00</modified>
  <entry> 
    <title>Entry1</title>
    <link rel=""alternate"" type=""text/html"" href=""http://xmlpitstop.com/entry1"" />
    <id>http://xmlpitstop.com/entry1</id>
    <created>2004-11-20T14:16:00-05:00</created>  
    <issued>2004-11-20T14:16:00-05:00</issued>  
    <modified>2004-11-20T14:16:00-05:00</modified>
    <content type=""text/html"" mode=""escaped"">Description of entry 1.</content>
  </entry>
  <entry>
    <title>Entry2</title>
    <link rel=""alternate"" type=""text/html"" href=""http://xmlpitstop.com/entry2"" />
    <id>http://xmlpitstop.com/entry2</id>
    <created>2004-11-20T14:16:00-05:00</created>
    <issued>2004-11-20T14:16:00-05:00</issued>
    <modified>2004-11-20T14:16:00-05:00</modified>
    <content type=""text/html"" mode=""escaped"">Description of entry 2.</content>
  </entry>
</feed>

Reading the RSS feed

This class basically works like the Atom class. The only difference is that it is designed for the RSS feed. To read a RSS feed I have used the same function as for the Atom feed. Again this function will return you a dataset. All you need to do is to create an object of the RSS class and call the function. In my example I used this code to bind my datagrid with the RSS feed.

'' Create rss object
Dim objRSS As New RSS.RSSFeed
Private Sub Page_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
  SetRSSFeedToDataGrid()
End Sub
Private Function SetRSSFeedToDataGrid()
  '' set rss file
  objRSS.FilePath = ""rss.xml""
  '' Set new dataset
  Dim ds As New DataSet
  '' read the feed 
  ds = objRSS.ReadRSSFeed()
  '' set dataset to the datagrid
  DataGrid2.DataSource = ds
  '' bind datagrid
  DataGrid2.DataBind()
End Function

Writing the RSS feed

As you have seen writing an Atom feed wasn’t that difficult at all. Writing a RSS feed is also easy and doesn’t take more time the writing an atom feed.

objRSS.FilePath = ""rss.xml""
objRSS.WriteStartElement(""XMLPitStop"", _
  ""http://www.xmlpitstop.com"", _
  ""Largest Resource of XML examples on the Web"", _
  ""All Rights reserver by XMLPitStop.com"", _
  ""60"")
objRSS.CreateItem(""Item1"", _
  ""http://www.xmlpitstop.com/link1"", _
  ""Description of Item1"", _
  DateTime.Now.ToString(""r""))
objRSS.CreateItem(""Item2"", _
  ""http://www.xmlpitstop.com/link2"", _
  ""Description of Item2"", _
  DateTime.Now.ToString(""r""))
objRSS.WriteEndElement()

Room for Improvements

Although these classes are easy to use and implement, there is plenty room for improvements. In instance I am not doing any error checking for the data. The dates for the atom feed needs to be checked if they are in the correct format or not. An interface could be provided, which accepts the data for the feeds and create it from there and so on….

Conclusion

In this article I have shown you how easily you can implement some classes with the .net framework to generate feeds within minutes. Although there is lot of room for improvement in the classes I do hope that the article helped you.

Related Links

Atom.NET
Is ATOM really better than RSS?
Sharing Web Content Using RSS
Custom ASP.NET Server Control for Displaying RSS Feeds
Consuming an RSS Feed with ASP.NET


 

 
Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!”



Advertise on XMLPitstop

Advertise on XMLPitstop


EggHead Cafe
Web Servicee development
DotNetSlackers
Ink refills
optimum rewards
help desk IT
premiereglobal
chicago web design
Alojamiento de Web
UK Web Hosting
IP Video Surveillance Software
Dolce&gabbana sunglasses
Video Surveillance
VoIP Internettelefonie AT

4,136 Total Members
9 members(last 30 days)
1 members(last 7 days)
0 members(today)

1,948 Total Discussions
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

47,487 Total Blog Posts
0 Blogs(last 30 days)
0 Blogs(last 7 days)
0 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

14,139 Total Resources
1 Resources(last 30 days)
0 Resources(last 7 days)
0 Resources(today)


 

David Silverlight's XMLPitstop.com| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139