Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#

How to Read Twitter Feeds With LINQ to XML

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
19 Apr 2009CPOL 21.2K   12   2
Twitter feeds are provided in RSS XML format. This makes it very easy for us to parse out the information we want from a feed using LINQ to XML.

Twitter feeds are provided in RSS XML format. This makes it very easy for us to parse out the information we want from a feed using LINQ to XML. For example, if we want to grab the message and date of each Twitter entry, we could use something like this:

C#
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;
    }
}

You can get the Twitter feeds by using the following:

C#
var fromTwitter = Twitter.Parse("Merlin981");

License

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


Written By
Software Developer (Senior)
United States United States
Winner - Best Mobile App - AT&T Developer Summit, Las Vegas, 2013

My personal resume can be found at: http://www.philippiercedeveloper.com

My game portfolio can be found at: http://www.rocketgamesmobile.com

About Philip Pierce:

I am a software developer with twenty years experience in game development, mobile, web, desktop, server, and database. My extensive background highlights an expertise in rapid application development using the latest Microsoft, Mobile, and Game Development technologies, along with the ability to create AI for games and business software, redesign existing software, develop multi-threaded software, and create client/server applications.

Comments and Discussions

 
QuestionWhere did you use LINQ ? Pin
bjuretko28-Apr-09 4:05
bjuretko28-Apr-09 4:05 
Questionwhy use loop? Pin
Grzesiek19-Apr-09 12:22
Grzesiek19-Apr-09 12:22 
IMHO better solution:

public class MyTwitter
{
    public string Message { get; set; }
    public DateTime PubDate { get; set; }

    public static IEnumerable<MyTwitter> Parse(string User)
    {
        var url = "http://twitter.com/statuses/user_timeline/" + User + ".rss";

        var element = XElement.Load(url);
        var rv = from node in element.Element("channel").Elements("item")
                 select new MyTwitter
                 {
                    Message = node.Element("description").Value.
                        Replace(User + ": ", string.Empty),
                    PubDate = DateTime.Parse(
                        node.Element("pubDate").Value)
                 };

        return rv;
    }
}


Grzesiek Danowski

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.