Its nice to be able to pull our latest tweets from twitter and place them anywhere on our page. This quick tutorial will show you exactly what you need to do in order to do just that!
Create a new class file. I called mine twitter.cs, but you can name it whatever you want.
Next, you need to declare your usings for this class. I used LINQ for this tutorial, but you can use XML by itself (which maybe I can create another tutorial for at a later date). So you will want to use System.Data, System.Linq, and System.XML.Linq. It should look like below:
using System.Data; using System.Linq; using System.Xml.Linq;/// <summary> /// Twitter Feed Reader /// </summary>
On another note, you will notice below the XML comment tags. Visual Studio fully supports XML Comments in so much that it not only uses them for comments, but also for declaring those nice little helper tips you see in the intellisense code. USE THEM! It makes your life easier. so here, you can see I declared my new class called “twitter”. This is the class which will be called in the page code if using it in code behind, or which will be used as a one way data object. Yes, this class can be used to include writing to twitter, but this is a simple and quick tutorial
The first part of the class is a whole mess of comments. Basically, it is a brief description of what the class does, what each parameter requires, what is returned, and any additional remarks. I wrote in remarks “Implement cache of code”. What that means to me (since I wrote it) is to implement a bit of data caching. Twitter has a max request limit. I am going to be adding in a bit of code at a later date (probably sometime next week) to control the amount of requests which the application overall sends to twitter, and which just shows my latest tweet for the period of at least 5 minutes before requesting another one.
public class twitter { /// <summary> /// Retrieves a set number of tweets based on the username and tweet count requested. /// Each full tweet set response (all tweets requested) is separated by an "|". /// Each part of each tweet is separated by an ";" /// </summary> /// <param name="tweetcount">Number of tweets to return</param> /// <param name="username">Username of twitterer</param> /// <remarks>Implement cache of code</remarks> /// <returns> /// DataTable, 4 Columns: /// Column 1 - ID /// Column 2 - Tweet Date /// Column 3 - Tweet Text /// Column 4 - Where Updated From /// </returns>continue with tutorial for this portion…}
So after the comments, we have a function called getTweet() which returns itself as a DataTable. Why DataTable? Because we need a DataObject for our ObjectDataSource! By returning a DataTable, we can then convert this class into a functional business object which can be dropped into any page using a myriad of data visualizers, such as DataGrid, GridView, ListView, Repeater, etc… (PS. Also makes for easy styling in the end, and less work in the code behind!) You will also notice there are two variables needed for this class, username and tweetcount. The username is… well… the username of the twitter user you want to retrieve. The tweetcount is the number of tweets you wish to go back. 5 is usually a good number for most personal web sites, and maybe a list of the latest 20 for business sites. The max value is 2000 I believe.
public DataTable getTweet(string username, int tweetcount) { DataTable tweetTable = new DataTable(); tweetTable.Columns.Add("tweetid",typeof(int)); tweetTable.Columns[0].AutoIncrement = true; tweetTable.Columns[0].AutoIncrementSeed = 1; tweetTable.Columns[0].AutoIncrementStep = 1; tweetTable.Columns[0].Unique=true; tweetTable.Columns[0].ReadOnly = true; tweetTable.Columns.Add("tweetDate",typeof(string)); tweetTable.Columns.Add("tweetText",typeof(string)); tweetTable.Columns.Add("tweetFrom",typeof(string)); return tweetTable; }
Next comes our LINQ to XML portion. It sounds more complicated than it is. There is a feature in VS which allows for intellisense in LINQ by the way, but I can never get it to work, so I test my queries the old fashioned way. On a positive note, its relatively painless to work with, and once your used to the syntax (and if your like me, you keep developer cheat sheets) its easy enough.
Now, the statusUri is a the web link to twitters user timeline. Simply put, it takes many variables, but for the sake of this tutorial, we are only supplying username and tweetcount.
Declare your xdocument with whatever name you wish as a new XDocument. Then set your XDocument to load the statusUri string variable.
The more pain in the rear part is the actual linq below. This is a simple example, but it can be a lot more complex.
string statusUri = "http://twitter.com/statuses/user_timeline.xml?screen_name=" + username + "&count=" + tweetcount; XDocument tdoc = new XDocument(); tdoc = XDocument.Load(statusUri); var statuses = from status in tdoc.Descendants("status") select new { tweetDate = status.Element("created_at").Value, tweetText = status.Element("text").Value, tweetFrom = status.Element("source").Value };
Lastly, do a for each loop for each status within the statuses from the XML file retrieved and parsed via LINQ and add it to the datatable as a new row! Its that simple.
foreach (var status in statuses) { DataRow dr = tweetTable.NewRow(); dr.SetField(1, status.tweetDate); dr.SetField(2, status.tweetText); dr.SetField(3, status.tweetFrom); tweetTable.Rows.Add(dr); }
Lastly, here is the entire code block for the object rolled up into one nice neat package![]()
using System.Data; using System.Linq; using System.Xml.Linq; /// <summary> /// Twitter Feed Reader /// </summary> public class twitter { /// <summary> /// Retrieves a set number of tweets based on the username and tweet count requested. /// Each full tweet set response (all tweets requested) is separated by an "|". /// Each part of each tweet is separated by an ";" /// </summary> /// <param name="tweetcount">Number of tweets to return</param> /// <param name="username">Username of twitterer</param> /// <remarks>Implement cache of code</remarks> /// <returns> /// DataTable, 4 Columns: /// Column 1 - ID /// Column 2 - Tweet Date /// Column 3 - Tweet Text /// Column 4 - Where Updated From /// </returns> public DataTable getTweet(string username, int tweetcount) { string statusUri = "http://twitter.com/statuses/user_timeline.xml?screen_name=" + username + "&count=" + tweetcount; XDocument doc = new XDocument(); doc = XDocument.Load(statusUri); var statuses = from status in doc.Descendants("status") select new { tweetDate = status.Element("created_at").Value, tweetText = status.Element("text").Value, tweetFrom = status.Element("source").Value }; DataTable tweetTable = new DataTable(); tweetTable.Columns.Add("tweetid",typeof(int)); tweetTable.Columns[0].AutoIncrement = true; tweetTable.Columns[0].AutoIncrementSeed = 1; tweetTable.Columns[0].AutoIncrementStep = 1; tweetTable.Columns[0].Unique=true; tweetTable.Columns[0].ReadOnly = true; tweetTable.Columns.Add("tweetDate",typeof(string)); tweetTable.Columns.Add("tweetText",typeof(string)); tweetTable.Columns.Add("tweetFrom",typeof(string)); foreach (var status in statuses) { DataRow dr = tweetTable.NewRow(); dr.SetField(1, status.tweetDate); dr.SetField(2, status.tweetText); dr.SetField(3, status.tweetFrom); tweetTable.Rows.Add(dr); } return tweetTable; } }
I hope you enjoyed this tutorial and you find it useful for creating any xml based business object.


