Binding an ASP.NET Repeater to Delicious (RSS) and Twitter (Atom) Feeds

Bookmark and Share

I recently had a client who wanted to pull in and display some Twitter and Delicious feeds on their web site. While I'd never done this before, it turns out that it's relatively simple to do by binding an ASP.NET Repeater control to the actual XML feed itself. I thought I'd share the code in case anyone else needs to do the same thing.

We start by creating our repeater objects, one for Twitter and one for Delicious. Since the feeds are different, and because Twitter uses Atom while Delicious uses RSS, you'll notice that the syntax is slightly different. There are also two Literal controls which we'll use for error catching in case either of the feeds ever has a problem.

// Recent Tweets

<%# (Container.DataItem as System.Xml.XmlNode)["content"].InnerText%>

<%# DateFormat((Container.DataItem as System.Xml.XmlNode)["published"].InnerText)%> by <%# TwitterName((Container.DataItem as System.Xml.XmlNode)["author"].ChildNodes[0].InnerText) %>

// Recent Bookmarks

<%# (Container.DataItem as System.Xml.XmlNode)["title"].InnerText%>

<%# DateFormat((Container.DataItem as System.Xml.XmlNode)["pubDate"].InnerText) %> by <%# (Container.DataItem as System.Xml.XmlNode)["dc:creator"].InnerText %>

In our code-behind file, all we're doing is creating an XmlDocument object and binding our repeaters to it. The Delicious feed works by default but since the Twitter feed uses Atom we need to invoke the XMLNamespaceManager object and load the Atom definition. The Try/Catch blocks just ensure that the feeds are working and if not, display the error message instead.

    public string twitterUrl = "http://search.twitter.com/search.atom?q=asp.net";
    public string deliciousUrl = "http://feeds.delicious.com/v2/rss/tag/aspnet?count=15";

    public System.Xml.XmlDocument doc;

    protected void Page_Load(object sender, EventArgs e)
    {
        doc = new System.Xml.XmlDocument();

        try
        {
            doc.Load(twitterUrl);
            XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
            nsMgr.AddNamespace("def", "http://www.w3.org/2005/Atom");
            rptTwitter.DataSource = doc.SelectNodes("/def:feed/def:entry[position()<=5]", nsMgr);
            rptTwitter.DataBind();
        }
        catch
        {
            rptTwitter.Visible = false;
            ltlTwitter.Visible = true;
        }

        try
        {
            doc.Load(deliciousUrl);
            rptDelicious.DataSource = doc.SelectNodes("/rss/channel/item[position()<=5]");
            rptDelicious.DataBind();
        }
        catch
        {
            rptDelicious.Visible = false;
            ltlDelicious.Visible = true;
        }

    }

Lastly, since the client wanted the dates to display as "3 hours ago" instead of the actual date and time, the following function handles that. There's also a very simple function to pull the Twitter user name.

    protected string DateFormat(string rawDate)
    {
        DateTime currentDate = DateTime.Now;
        DateTime itemDate = DateTime.Parse(rawDate);

        TimeSpan difference = currentDate - itemDate;

        string dateValue;
        if (difference.TotalSeconds <= 60)
        {
            dateValue = difference.Seconds + " seconds ago";
        }
        else if (difference.TotalMinutes <= 60)
        {
            dateValue = difference.Minutes + " minutes ago";
        }
        else if (difference.TotalHours <= 24)
        {
            dateValue = difference.Hours + " hours ago";
        }
        else
        {
            dateValue = difference.Days + " days ago";
        }

        return dateValue;
    }

    protected string TwitterName(string rawName)
    {
        return rawName.Substring(0, rawName.IndexOf(" "));
    }

That's it. Simple, but it works. You can download a zip of these files to use if you like. It also contains some very basic CSS and an OutputCache declaration so that the page isn't hitting external URLs on every single page load.

Credit where credit is due... I hit up others sites, blogs, and forum posts to get ideas for how to do this and pulled together bits and pieces from various places. So thanks to the development community at large for always sharing information like this. I hope this post helps someone else as well.

Tags

// // // // //

Very cool, Thanks for the code lesson. I recently changed up my company blog from wordpress to Blogengine.net on ASP.NET and love how easy it is to program. The twitter updates are now a extension, but I had not seen how to implement Delicious. You made it look easy.

I'm off to build a "tweet this" button for posts.

Thanks! Was able to reuse this code, worked great!

No TrackBacks
TrackBack URL: http://www.codescene.com/cgi-bin/mt/mt-tb.cgi/100

Leave a Comment

Free the web - Boycott Internet Explorer 6