Suppose we have a model as under
public class RSS
{
public string Id { get; set; }
public string QuestionID { get; set; }
public string QuestionTitle { get; set; }
public string QuestionDescription { get; set; }
public DateTime PublishDate { get; set; }
}
The below code will read RSS feed by using SyndicationFeed Class in C#
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Xml;
namespace ConsoleApplication1
{
public class RSSReader
{
public IEnumerable<RSS> GetRSSFeeds()
{
string url = <RSS FEED URL>;
SyndicationFeed feed = null;
using (var reader = XmlReader.Create(url))
{
feed = SyndicationFeed.Load(reader);
var syndicationItems = feed.Items.ToArray();
for (int i = 0; i < syndicationItems.Length; i++)
{
yield return new RSS
{
QuestionID = syndicationItems[i].Id,
QuestionTitle = syndicationItems[i].Title.Text,
QuestionDescription = syndicationItems[i].Summary.Text,
PublishDate = syndicationItems[i].PublishDate.Date
};
}
}
}
}
}
As can be figure out that, we are using SyndicationFeed Class that represents a top-level feed object, <feed> in Atom 1.0 and <rss> in RSS 2.0. It resides under System.ServiceModel.Syndication namespace.We are using yield statement for maintaining a stateful iteration.