Processing Technorati using C# 3.0 and Linq

Following on from my previous post, I wondered how more effective and easily the processing could be done using the new features of C# 3.0 and Linq to XML. And it was…

When creating the code, the first thing to do is bring in the System.Xml.Linq assembly which can be found in C:Program FilesReference AssembliesMicrosoftFrameworkv3.5 – or at least this is the case with Orcas Beta 1.

We then create a XElement, and pass the URL for Technorati via the static method Load to allow it to download the query response.

1 string url = string.Format("http://api.technorati.com/tag?key={0}&tag={1}&limit={2}excerptsize={3}", technoratiAPI, technoratiTerm, limit, excerptsize); 2 XElement response = XElement.Load(url);

Below is the LINQ to Xml query to process each of the items returned, sorry about the wrapping – code link at the end of the post.  The first thing which is required is to state which Descendants are being queried, which in this case is all of the item elements. We then create objects of type Blog like before, however this time we can use the new feature of C# 3.0 called Object Initializers.  We then create a sub object of type Weblog for the details of who made the post.  This is then stored in the query variable.  One point is that you need to include .Value on the Element() as we are using a known object type instead of letting the compiler work it out.

1 var query = from post in response.Descendants("item") 2 select new Blog { Title = post.Element("title").Value, 3 Description = post.Element("excerpt").Value, 4 Created = Convert.ToDateTime(post.Element("created").Value), 5 Url = post.Element("permalink").Value, 6 Weblog = new Weblog { 7 Name = post.Element("weblog").Element("name").Value, 8 LastUpdated = Convert.ToDateTime(post.Element("weblog").Element("lastupdate").Value), 9 Rss = post.Element("weblog").Element("rssurl").Value, 10 Url = post.Element("weblog").Element("url").Value 11 } 12 };

As our method is returning a generic list of Blog objects, we need to export the query to do this.  This is done very simply, by query.ToList() which causes it to execute the query, and convert the result to a generic collection of type .

1 List<Blog> GetTechnoratiPosts() 2 return query.ToList<Blog>();

Once we have it in this format, we can use it the same as we did before, using extractly the same ASP.net code to display the query on the screen. 

One more point, as we are using C# 3.0, we can also use the new Automatic Properties feature to cut down the amount of code required to create the simply blog and weblog objects.  The code for the blog class is now:

1 public class Blog 2 { 3 public Weblog Weblog { get; set; } 4 public string Title { get; set; } 5 public string Description { get; set; } 6 public DateTime Created { get; set; } 7 public string Url { get; set; } 8 }

Very nice, saves a lot of unnecessary typing.

Hope you find this useful, I think its cool.

Code can be downloaded:
http://blog.benhall.me.uk/code/CSharp/Technorati/TechnoratiLinqToXml.txt

Technorati tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *