<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
<status>
<created_at>Tue Apr 09 10:08:15 +0000 2013</created_at>
<id>1016846548</id>
<text>Hi "everybody" #greetings</text>
<source>web</source>
<truncated>true</truncated>
<in_reply_to_status_id></in_reply_to_status_id>
<in_reply_to_user_id></in_reply_to_user_id>
<favorited>false</favorited>
<user>
<id>2566935</id>
<name>Abc Xyz</name>
<screen_name>Abc</screen_name>
<location></location>
<description></description>
<profile_image_url>http://.../normal.jpg</profile_image_url>
<url>https://www.blogger.com/blogger</url>
<protected>false</protected>
<followers_count>42</followers_count>
</user>
</status>
<status>
...
</status>
</statuses>
Extracting the Data
XDocument document = XDocument.Parse(response, LoadOptions.None);
var query = from e in document.Root.Descendants("status")
select new UserStatus
{
UserName = e.Element("user").Element("name").Value,
ProfileImage = e.Element("user").Element("profile_image_url").Value,
Status = HttpUtility.HtmlDecode(e.Element("text").Value),
StatusDate = (e.Element("created_at").Value.ParseDateTime())
};
Few things to note about the above code:
1.
HttpUtility.HtmlDecode is being used in order to get a readable text. The response contained text like “Testing "posts" #devacademy3” and this method (in System.Web) converts it to
“Testing “posts” #devacademy3”.
2. The
ParseDateTime() method on the element’s value is an extension method I created in order to parse a DateTime from the special string representation of the time the status was posted:
Sat Nov 15 10:08:15 +0000 2008
This method looks like:
public static class StringExtensions
{
public static DateTime ParseDateTime(this string date)
{
string dayOfWeek = date.Substring(0, 3).Trim();
string month = date.Substring(4, 3).Trim();
string dayInMonth = date.Substring(8, 2).Trim();
string time = date.Substring(11, 9).Trim();
string offset = date.Substring(20, 5).Trim();
string year = date.Substring(25, 5).Trim();
string dateTime = string.Format("{0}-{1}-{2} {3}", dayInMonth, month, year, time);
DateTime ret = DateTime.Parse(dateTime);
return ret;
}
}