Archive

Archive for February, 2011

Atom and Syndication

February 12, 2011 Leave a comment

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());
}
}
};

Categories: C#, LINQ Tags: , ,
Follow

Get every new post delivered to your Inbox.