Wriju's BLOG

.NET, Cloud and everything

Windows Phone 7 : Reading Twitter Timeline

This one is similar to my previous post on Reading RSS. But useful in Phone App.

So you need the structure as below

public class TWClass
    public string TextMSG { get; set; }
    public string CreatedDt { get; set; }

After that you need the below code to get your Twitter timeline. My Twitter handle is wriju_ghosh. I have hardcoded it. You can make it dynamic as well.

public Page1()
    WebClient myTweet = new WebClient();
    myTweet.DownloadStringCompleted += new DownloadStringCompletedEventHandler(myTweet_DownloadStringCompleted);

    myTweet.DownloadStringAsync(new Uri(@"http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=wriju_ghosh"));

void myTweet_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    //Need to check Network/WiFi etc before proceeding further..
    var TWData = from tw in XElement.Parse(e.Result).Descendants("status")
                    select new TWClass
                        TextMSG = tw.Element("text").Value,
                        CreatedDt = tw.Element("created_at").Value
    lstTW.ItemsSource = TWData;    

Now UI would look like (this can be better).

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <ListBox Name="lstTW" Margin="0,0,6,0">
                <StackPanel Width="400">
                    <TextBlock Text="{Binding Path=TextMSG}"></TextBlock>
                    <TextBlock Text="{Binding Path=CreatedDt}"></TextBlock>
                    <TextBlock Text="----"></TextBlock>