Atom and Syndication

I wanted to write a program to read tweets.  There are easy to use REST based API to give you JSON or ATOM feed. There are many ways to read XML, using XMLReader, XML Serialization etc. However, I was not knowing how easy it is to read any atom feed using the syndication API of .NET.  So here it is …


using System;
using System.Linq;
using System.Xml;
using System.ServiceModel.Syndication;

namespace ReadTwitter
{
class Program
{
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(@http://api.twitter.com/1/statuses/user_timeline.atom?screen_name=rafatsarosh);
SyndicationFeed feed = SyndicationFeed.Load(reader);
var tweetItems = from item in feed.Items
            select new
                {
                item.Title.Text,
                item.PublishDate
                 };
foreach (var o in tweetItems)
 {
Console.WriteLine(o.Text );
     Console.WriteLine(o.PublishDate);
 }
 Console.Read();
 }
 }
}

Here is another alternative for windows phone, as we don’t have SyndicationFeed object yet in windows phone.

WebClient wc = new WebClient();
XmlReader reader = XmlReader.Create ( wc.OpenRead(@"http://api.twitter.com/1/statuses/user_timeline.atom?screen_name=rafatsarosh"));
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "content")
{
Console.WriteLine(reader.ReadInnerXml());
}
}
};
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s