Created
March 23, 2011 03:02
-
-
Save azcoov/882541 to your computer and use it in GitHub Desktop.
Read a Twitter feed with Linq to XML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TwitterSearch | |
{ | |
private const string urlTemplate = "http://search.twitter.com/search.atom?q={0}"; | |
private static XNamespace atomNS = "http://www.w3.org/2005/Atom"; | |
public static List<Tweet> Query(string query) | |
{ | |
XDocument xDoc = XDocument.Load(string.Format(urlTemplate, query)); | |
var tweets = | |
(from tweet in xDoc.Descendants(atomNS + "entry") | |
select new Tweet | |
{ | |
Title = (string)tweet.Element(atomNS + "title"), | |
Published = | |
DateTime.Parse((string)tweet.Element(atomNS + "published")), | |
Id = (string)tweet.Element(atomNS + "id"), | |
Link = tweet.Elements(atomNS + "link") | |
.Where(link => (string)link.Attribute("rel") == "alternate") | |
.Select(link => (string)link.Attribute("href")) | |
.First(), | |
Author = (from author in tweet.Descendants(atomNS + "author") | |
select new Author | |
{ | |
Name = (string)author.Element(atomNS + "name"), | |
Uri = (string)author.Element(atomNS + "uri"), | |
}).First(), | |
}); | |
return tweets.ToList<Tweet>(); | |
} | |
public class Tweet | |
{ | |
public string Id { get; set; } | |
public DateTime Published { get; set; } | |
public string Link { get; set; } | |
public string Title { get; set; } | |
public Author Author { get; set; } | |
} | |
public class Author | |
{ | |
public string Name { get; set; } | |
public string Uri { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment