Wednesday, August 12, 2009

A simple Rss Feed Parser using LINQ To XML

Download Code

A simple class that can be used to parse rss feed, Check the code attached it will display my twitter updates by parsing the rss feed using the class.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
namespace RssFeed
{

//12-aug-2009 Priyan R
public class RssParser
{
public RssParser()
{
Items = new List<RssItem>();
}
public RssParser(string url):this()
{
URL = url;
}
#region Methods
public void Parse()
{
XDocument doc = XDocument.Load(URL, LoadOptions.None);
Items = (from t in doc.Descendants("item")
select new RssItem()
{
Title = t.Element("title").Value,
Description = t.Element("description").Value,
Link = t.Element("link").Value

}
).ToList();

}
#endregion
#region properties
public string URL { get; set; }
public List<RssItem> Items { get; set; }
#endregion
}
#region RSS Item Class
public class RssItem
{
public string Title{ get; set; }
public string Description { get; set; }
public string Link { get; set; }
}
#endregion
}

1 comment:

Oguz said...

If the site uses feedburner you can't take original URL Address
you should use this code

var x = (from it in XD.Descendants("item")
orderby DateTime.Parse(it.Element("pubDate").Value) ascending
where DateTime.Parse(it.Element("pubDate").Value) > prmLastEntry
select it.Elements()).ToArray();
// and

foreach (var itemx in x)
{
foreach (var itemsub in itemx)
{
if (itemsub.Name == "{http://rssnamespace.org/feedburner/ext/1.0}origLink")
{
string OriginalLink = itemsub.Value;
}
}
}