Click here to Skip to main content
15,434,829 members
Articles / Web Development / ASP.NET
Tip/Trick
Posted 10 Jun 2015

Stats

8.5K views
3 bookmarked

Consume an Atom Feed in an ASP.NET Web Page

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
10 Jun 2015CPOL2 min read
Consuming / displaying an Atom feed on an ASP.NET web page

Introduction

Following on from my previous article on consuming an RSS feed, I recently had a requirement to display the contents from an Atom feed on a web page. In fact this was to display the contents of my very own Github activity feed on my web site.

I won't compare and contrast RSS with Atom in this article. There are plenty of sources out there that have alredy done that.

Displaying an Atom feed on a web site is relatively simple. In its simplest terms an Atom feed is an XML structure that contains data which can be displayed on a web page. Atom feeds are used by many different kinds of web sites to push information. Users who are interested in that information can then subscribe to those Atom feeds by consuming them.

Structure of an Atom feed

Atom feeds have the following basic structure. Other information is contained within them but for the purposes of brevity these have been removed.

XML
<feed xmlns="http://www.w3.org/2005/Atom">
 
	<title>Example Feed</title>
	<subtitle>A subtitle.</subtitle>
	<link href="http://example.org/feed/" rel="self" />
	<link href="http://example.org/" />
	<id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id>
	<updated>2003-12-13T18:30:02Z</updated>
 
 
	<entry>
		<title>My Atom feed</title>
		<link href="http://example.org/2003/12/13/atom03" />
		<link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"/>
		<link rel="edit" href="http://example.org/2003/12/13/atom03/edit"/>
		<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
		<updated>2003-12-13T18:30:02Z</updated>
		<summary>Some summary text.</summary>
		<content type="xhtml">
			<div xmlns="http://www.w3.org/1999/xhtml">
				<p>This is the entry content in HTML format.</p>
			</div>
		</content>
		<author>
			<name>Dominic Burford</name>
			<email>example@mydomain.co.uk</email>
		</author>
	</entry>
</feed>

The information that I was interested in for my web site were the Title, Description and Publication Date items from the Atom feed.

Consuming an Atom feed

There are several different ways to achieve this, so this is just one way. I have implemented this using an ASP.NET web form but the general principle can be applied to other technologies.

  1. Firstly I added a Gridview control to my web page. A Repeater control is also perfectly acceptable,
    ASP.NET
    <asp:GridView ID="gvAtom" runat="server">
      <Columns>
        <asp:TemplateField>
          <ItemTemplate>
            <table width="100%" border="0" cellpadding="0" cellspacing="5">
              <tr>
                <h3 style="color:#3E7CFF"><%#Eval("Title") %></h3>
              </tr>
              <tr>
                <%#Eval("PublishDate") %>
              </tr>
              <tr>
                <td colspan="2">
                  <hr />
                  <%#Eval("Description") %>
                </td>
              </tr>
            </table>
          </ItemTemplate>
        </asp:TemplateField>
      </Columns>
    </asp:GridView>
  2. In the code behind I have added a new method to populate the Gridview control with data from the Atom feed as follows.
    C#
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Globalization;
    using System.Linq;
    using System.Xml.Linq;
    using System.ServiceModel.Syndication;
    using System.Xml;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        this.PopulateAtomFeed();
    }
    
    private void PopulateAtomFeed()
    {
        string atomFeedUrl = ConfigurationManager.AppSettings["AtomFeedUrl"];
        List<Feeds> feeds = new List<Feeds>();
        XmlTextReader reader = new XmlTextReader(atomFeedUrl);
        SyndicationFeed feed = SyndicationFeed.Load(reader);
    
        if (feed != null)
        {
            feeds.AddRange(feed.Items.Select(i => new Feeds
            {
                Title = i.Title.Text,
                PublishDate = i.PublishDate.DateTime.ToUniversalTime().ToString(CultureInfo.InvariantCulture),
                Description = ((TextSyndicationContent)(i.Content)).Text
    
            }));
        }
        gvAtom.DataSource = feeds;
        gvAtom.DataBind();
    }
  3. I then created a simple POCO Feed class that mapped the Atom feed items to the Gridview control.
    C#
    public class Feeds
    {
        public string Title { get; set; }
        public string PublishDate { get; set; }
        public string Description { get; set; }
    }
  4. I store the URL to my Atom feed in the web.config. This ensures that the URL can be easily changed if necessary.
    XML
    <appSettings>
      <add key="AtomFeedUrl" value="http://www.somedomain.com/Atomfeed.Atom"/>
    </appSettings>

Summary

And that's all there is to it. As can be seen from the link I provided you can view the Atom feed yourself on my web site.

If you are likely to display multiple Atom feeds then you can easily create a User Control and add the Gridview to this. To make the User Control reusable you could add a public property to the User Control to set the Atom feed URL. You then add the User Control to your web page and set the Atom feed URL property as required.

Hopefully this article has given you sufficient information for you to start consuming Atom feeds in your own applications. Feel free to leave a comment if you would like me to further elaborate on anything within this article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Gold-Vision CRM
United Kingdom United Kingdom
I am a professional software engineer and technical architect with over twenty years commercial development experience with a strong focus on the design and development of web and mobile applications.

I have experience of architecting scalable, distributed, high volume web applications that are accessible from multiple devices due to their responsive web design, including architecting enterprise service-oriented solutions. I have also developed enterprise mobile applications using Xamarin and Telerik Platform.

I have extensive experience using .NET, ASP.NET, Windows and Web Services, WCF, SQL Server, LINQ and other Microsoft technologies. I am also familiar with HTML, Bootstrap, Javascript (inc. JQuery and Node.js), CSS, XML, JSON, Apache Cordova, KendoUI and many other web and mobile related technologies.

I am enthusiastic about Continuous Integration, Continuous Delivery and Application Life-cycle Management having configured such environments using CruiseControl.NET, TeamCity and Team Foundation Services. I enjoy working in Agile and Test Driven Development (TDD) environments.

Outside of work I have two beautiful daughters. I am also an avid cyclist who enjoys reading, listening to music and travelling.

Comments and Discussions

 
-- There are no messages in this forum --