Click here to Skip to main content
15,867,686 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.50/5 (3 votes)
2 Apr 2009CPOL 28.4K   1   13   7
In this post, you will learn how to read Twitter feeds with 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

 
QuestionSeems not to work anymore Pin
CodeBetweenTheLines30-Nov-12 9:00
CodeBetweenTheLines30-Nov-12 9:00 
GeneralMy vote of 5 Pin
version_2.029-Sep-11 21:31
version_2.029-Sep-11 21:31 
GeneralIt's not reading Pin
cyberion8418-May-10 15:33
cyberion8418-May-10 15:33 
GeneralRe: It's not reading Pin
Tolupoint4-Sep-12 7:30
Tolupoint4-Sep-12 7:30 
Generalhi Pin
mbaocha25-Aug-09 11:42
mbaocha25-Aug-09 11:42 
GeneralOr more concisely ... using a few more LINQ features ... Pin
HightechRider2-Apr-09 9:35
HightechRider2-Apr-09 9:35 
GeneralRe: Or more concisely ... using a few more LINQ features ... Pin
Tolupoint4-Sep-12 8:07
Tolupoint4-Sep-12 8:07 

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.