In this article i will show how to read feeds rss.
In this example i will show it for MVC architecture but it's very easy to use it in webforms. (use the SyndicationFeed object as a datasource for an repeater for example).
Let's declare our SyndicationFeed object in Model:
public class BlogModel
{
public SyndicationFeed BlogFeed { get; set; }
}
In Controller we'll get the feeds. But first, we need to add new references to our project: System.ServiceModel.Syndication e System.Xml
public ActionResult Index()
{
var model = new BlogModel();
string strFeed = "http://pontonetpt.com/blogs/guilhermecardoso/rss.aspx";
using (XmlReader reader = XmlReader.Create(strFeed))
{
SyndicationFeed rssData = SyndicationFeed.Load(reader);
model.BlogFeed = rssData;;
}
return View(model);
}
Now we will read the feeds in View, through a foreach cicle. In this example, i'm only displaying the last 5 entries from my blog.
<% if(Model.BlogFeed!=null) { %>
<ul>
<% foreach (var post in Model.BlogFeed.Items.ToList().Take(5)) { %>
<li>» <a href="<%=post.Links.First().Uri%>" target="_blank"><%=post.Title.Text %></a></li>
<% } %>
Yoi can see more details about SyndicationFeed class here.