ASP.NET Tip: Creating an Atom XML Feed

An easy way to promote your site and to attract more visitors is to provide a news feed. Feeds can be read by a variety of programs, including FeedDemon, Mozilla, and the upcoming Internet Explorer 7.0. Many sites continue to use RSS (Real Simple Syndication), but RSS was never a defined standard. Atom, the replacement for RSS, is quickly being adopted by many of the blogging software makers, although most of them still provide support for RSS as well.

This tip shows you how to produce an Atom XML feed using a database table. It is based on the following sample of the Atom syndication format, which is available on this page:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

   <title>Example Feed</title>
   <link href="http://example.org/"/>
   <updated>2003-12-13T18:30:02Z</updated>
   <author>
      <name>John Doe</name>
   </author>
   <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>

   <entry>
      <title>Atom-Powered Robots Run Amok</title>
      <link href="http://example.org/2003/12/13/atom03"/>
      <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
      <updated>2003-12-13T18:30:02Z</updated>
      <summary>Some text.</summary>
   </entry>

</feed>

The entry tag section is the one that will repeat for each article you want to syndicate. Most of the information is fairly self-explanatory, and the atomenabled.org site provides other tags that let you further document your articles. A few of these tags may require a bit of coding on your part, however.

The first tag you see is the updated tag, which shows the time in the standard Internet format. The format shows "Zulu" time, which is a military/aviation name for Greenwich Mean Time. Although you could manually create this format, some built-in date/time formats will get you most of the way to there.

The first format string is a lowercase s, which generates the date and time in this format:

2000-08-17T16:32:32

The second option is a lowercase u, which generates the date and time in this format:

2000-08-17 23:32:32Z

The second option also translates the local time to the "universal" time. Because you need both the T and the Z, as well as the timezone conversion, you can use the ToUniversalTime method of the DateTime class, the lowercase s format, and simply append a Z to the string. (You can learn more about the various built-in time/date formats by looking at the ToString method of the DateTime class in the MSDN documentation.)

The next tag is the ID tag on both the feed and the entry levels. This is easy to create in .NET by using a globally unique identifier (GUID). The important thing to remember is that this unique value should persist for this entry, meaning that you can't just generate random ones each time you display the feed. In my case, I store the unique identifier in the database so it always displays the same value. If you don't do that, it will cause the various newsreaders to display changes to an article as a new article, which defeats the purpose of sending a feed. If you keep the same ID, the newsreader will usually just indicate that the article has changed but not create a new entry.

Here's the code I use to generate my Atom feed:

protected void Page_Load(object sender, EventArgs e)
{
   // Code to retrieve DataTable of news articles
   StringBuilder feed = new StringBuilder();

   feed.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
   feed.Append("<feed xmlns=\"http://www.w3.org/2005/Atom\">");
   feed.AppendFormat("<title>{0}</title>", titlegoeshere);
   feed.Append("<link href=\"http://www.northcomp.com/\" />");
   feed.AppendFormat("<updated>{0:s}Z</updated>",
                     lastupdatedDateTime.ToUniversalTime());
   feed.Append("<author><name>Eric Smith</name></author>");
   feed.AppendFormat("<id>urn:uuid:{0}</id>",
                     uniqueIDForFeedThatStaysSame);

   foreach (DataRow dr in dt.Rows)
   {
      feed.AppendFormat("<entry><title>{0}</title>",
                        dr["Title"]);
      feed.AppendFormat("<link>http://www.northcomp.com/about/
                         news.aspx?id={0}</link>",
                         dr["pkArticleID"]);
      feed.AppendFormat("<id>urn:uuid:{0}</id>",
                        dr["AtomID"]);
      feed.AppendFormat("<summary>{0}</summary>",
                        dr["Synopsis"]);
      feed.AppendFormat("<updated>{0:s}Z</updated>",
                        Convert.ToDateTime(dr["ReleaseDate"]).
                        ToUniversalTime());
      feed.Append("</entry>");
   }
   feed.Append("</feed>");

   Response.Clear();
   Response.ContentType = "text/xml";
   Response.Write(feed.ToString());
   DB.Close();

}

The ASPX portion of this page should contain only the Page directive at the top of the file. My table contains an identity field called pkArticleID, which provides the unique link for each news entry. The AtomID field is a unique identifier, and the Title, Synopsis, and ReleaseDate fields are self-explanatory. At the feed level, I use the latest news article's release date as the updated value, as the data table I retrieve is sorted in reverse order by date—I just pull the first ReleaseDate field and put it in that field.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic. Send him your questions and feedback via e-mail at questions@techniquescentral.com.



Comments

  • wheloltabotly PumeSonee Phobereurce 6315153

    Posted by TizefaTaNaday on 06/03/2013 08:03am

    BroobrehagNer cheapjordanfusion6.holidaygiving.org Viectteaw airjordanretro3whitecementgrey.holidaygiving.org pusitonus

    Reply
  • wholesale hats

    Posted by dvexpenueMoxjef on 03/29/2013 11:17pm

    [url=http://www.cheapforsunglasses.com]cheap sunglasses[/url]cheap snapbacks from china [url=http://www.cheapforsunglasses.com]cheap sunglasses[/url]cheap snapbacks free shipping [url=http://www.cheapforsunglasses.com]cheap oakley[/url]cheap sunglasses [url=http://www.bestcheapsnapbacks.com]cheap snapbacks free shipping[/url]cheap oakley [url=http://www.bestwholesalehats.com]wholesale snapbacks[/url]cheap sunglasses

    Reply
  • snapback hats wholesale

    Posted by xtexpenueMoxjef on 03/29/2013 11:00pm

    [url=http://www.bestwholesalehats.com]snapback hats wholesale[/url]snapback hats wholesale [url=http://www.bestcheapsnapbacks.com]cheap snapbacks[/url]oakleys cheap [url=http://www.bestwholesalehats.com]snapback hats wholesale[/url]cheap snapbacks for sale [url=http://www.cheapforsunglasses.com]cheap sunglasses[/url]wholesale snapback hats [url=http://www.bestwholesalehats.com]snapback hats wholesale[/url]cheap snapbacks from china

    Reply
  • wholesale designer sunglasses

    Posted by kgliliImpumpzia on 03/29/2013 10:52am

    sunglasses cheap [url=http://onlineguciisunglass.webs.com]sunglasses cheap[/url] fake ray ban wayfarer discount oakley sunglasses,,,,,o [url=http://discountsunglassessale.webs.com]discount oakley sunglasses,,,,,o[/url] fake oakleys cheap fake oakley sunglasses [url=http://bestsunglassesshop.webs.com]cheap fake oakley sunglasses[/url] cheap ray ban sunglasses ray ban sunglasses cheap [url=http://sunglasspomoteauthentic.webs.com]ray ban sunglasses cheap[/url] discount ray ban cheap sunglasses [url=http://qualityguccisunglass.webs.com]cheap sunglasses[/url] fake ray ban wayfarer wholesale sunglasses china [url=http://olesalesunglassesgood.webs.com]wholesale sunglasses china[/url] cheap oakley frogskins cheap fake oakleys [url=http://bestsunglassesshop.webs.com]cheap fake oakleys[/url] oakley discount oakley discount [url=http://discountoakleysunglassesho.webs.com]oakley discount[/url] cheap wayfarer sunglasses

    Reply
  • cop costume

    Posted by Fishnetdi1109 on 03/29/2013 09:28am

    http://sexycostumesboutique.webs.com - Office Tramp LingerieBoxer sets have grown to be extremely popular nowadays It can be quite sensual since it brings the body contours to proportion and this is why the sizing matters http://sexylingerieshops.webs.com - Sexy Lingerie ShopInvite your co-workers, family and friends to join you Open an eBay storefront and increase your online auction sales http://sexycostumesus.webs.com - Sexy CostumesTherefore, you should choose a wholesale lingerie dress as per your body type and size and the style that suits your personality A pajama set might not sound too sexy, but if it is silky, and buttons up the front, it can make your woman feel very sexy, especially if she is a plus size (size 18 or larger) http://sexystockings.webs.com - black silk stockingsTip 9 – If you are going to be purchasing any lingerie on line it is best not to purchase through an auction site although the prices may be less In contrast of black, there is white which portrays a pure, clean and innocent image http://SexyChemise.webs.com - Lace ChemiseA bra that is too tight can leave a pinching red mark on your back caused by the tightened elastic It gives a cute look to the area being covered while it accents the hips and reveals a small area of the butt

    Reply
  • fake ray ban sunglasses

    Posted by dgliliImpumphef on 03/28/2013 10:09pm

    http://onlineguciisunglass.webs.com - cheap ray ban wayfarer fake oakleys http://guccisunglassescheap.webs.com - cheap sunglasses oakley sunglasses cheap http://sunglassdicountsaleu.webs.com - oakleys cheap discount ray ban sunglasses http://sunglassdicountsaleu.webs.com - ray ban wayfarer cheap wholesale sunglasses http://sunglassdicountsaleu.webs.com - designer sunglasses cheap cheap aviator sunglasses

    Reply
  • cheap ray ban

    Posted by mgliliImpumpguq on 03/28/2013 10:07pm

    http://discountsunglasseshoo.webs.com - discount ray ban ray ban sunglasses cheap http://sunglasspomoteauthentic.webs.com - cheap sunglasses wholesale sunglasses http://discountoakleysunglassesho.webs.com - discount sunglasses oakley discount http://qualityguccisunglass.webs.com - cheap ray ban sunglasses wholesale sunglasses http://wholesalesunglassescool.webs.com - wholesale oakley sunglasses oakleys for cheap

    Reply
  • fake oakley sunglasses

    Posted by mgliliImpumppuf on 03/28/2013 08:28pm

    http://akeoakleysunglasses.webs.com - fake oakley sunglasses cheap sun glasses http://akeoakleysunglasses.webs.com - fake ray ban fake oakley sunglasses http://akeoakleysunglasses.webs.com - fake ray ban wayfarer fake oakley sunglasses http://fakeGucciwayfarer.webs.com - fake ray ban oakley sunglasses cheap http://akeoakleysunglasses.webs.com - fake ray ban akley discount

    Reply
  • http://www.tomsoutletw.com/ seszej

    Posted by http://www.tomsoutletw.com/ Mandyeml on 03/28/2013 10:49am

    Snow smart, she began to discover to this mission is not simple, and clear the sixth sense told her crisis coming. Fat now? Him, but this time the most depressed person. Wanted to rely on his extraordinary strength, able to hand a little brother or two Xiaomi, mastermind of such an arrangement, too! It also makes him could not help but secretly sigh of unpredictable things. Well, the mastermind of the barrier disappears, ray ban wayfarer out. Blood wasteland east about 20 km away camp Rogge, since ray ban wayfarer sizes Select justice camp, where ray ban clubmaster must visit the. Well, oakley sunglasses out of it.oakley sunglasses discount, Xiao Feng noted that the mastermind of the unique barrier disappeared after that white light appeared. Psychological hold against the clock, Xiao Feng turned commanded said to the crowd.

    Reply
  • ugg boots eqzesi

    Posted by Mandysll on 02/19/2013 10:21am

    cheap ugg boots sale uhqubssz cheap ugg boots uk zbzzqdnn cheap ugg boots wocoelgy cheap uggs nvrgjzso ugg boots sale uk owpspyex ugg boots sale zquzongk ugg boots uk mailcozl ugg boots bngcbftu ugg sale srxaimrg

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds