Monthly Archives: January 2009

Simple class to parse Twitter with LINQ

In ParaPlan 4.0, we use twitter to maintain a change log.  I wanted to display this information to our users, so I wrote a little class that calls the RSS feed and uses LINQ to parse the data.  All I need is the message and the date, so that is all it pulls out.

Here is the class:

public class Twitter
{
    public string Message { get; set; }
    public DateTime PubDate { get; set; }
 
    public static List<Twitter> Parse(string User)
    {
        var rv = new List<Twitter>();
        var url = "http://twitter.com/statuses/user_timeline/" + User + ".rss";
 
        var element = XElement.Load(url);
        foreach (var node in element.Element("channel").Elements("item"))
        {
            var twit = new Twitter();
            var message = node.Element("description").Value;
            //remove username information
            twit.Message = message.Replace(User + ": ", string.Empty);
            twit.PubDate = DateTime.Parse(node.Element("pubDate").Value);
            rv.Add(twit);
        }
 
        return rv;
 
    }
}

Our calling code looks like this:

var changes = new List<string>();
 
var fromTwitter = Twitter.Parse("ParaPlan");
fromTwitter.ForEach(t =>
    changes.Add(t.PubDate.ToString("MM/dd/yy") + " - " + t.Message));
 
var list = new ListBox();
 
list.ItemsSource = changes;

 

Technorati Tags: ,,,,



Benjamin Allan Hibbard

On 12-20-2008, my wife Chelsea, gave birth to our beautiful baby boy Benjamin. It’s been a crazy exciting and busy couple of weeks so far. I’ve tried to respond to all the many people that have congratulated us on twitter and facebook, but if I forgot, thank you so much. All of your kind words and well wishes have been very appreciated!!

Many more pictures here.

Technorati Tags: