Geeks With Blogs
Guilherme Cardoso, Blog EN .NET geek

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.

Posted on Monday, October 18, 2010 11:48 PM | Back to top


Comments on this post: ASP.NET MVC - Reading feeds RSS

# re: ASP.NET MVC - Reading feeds RSS
Requesting Gravatar...
Obrigado! ;)
Left by Bruno on Jul 23, 2011 9:13 AM

# re: ASP.NET MVC - Reading feeds RSS
Requesting Gravatar...
Why don't you order it with linq?
Left by Guilherme Cardoso on Jul 23, 2011 9:16 AM

# re: ASP.NET MVC - Reading feeds RSS
Requesting Gravatar...
Gulherme, valeu pelo arqtigo ai cara ficou otimo era exatamente isso q eu procurava, mas agora sem querer abusar... vc joga os feeds em links, tem como jogar pra dentro de uma grid por exemplo uma grid mvc da devexpress?
Left by Fabio on Nov 25, 2011 12:00 AM

# re: ASP.NET MVC - Reading feeds RSS
Requesting Gravatar...
i'm new in MVC and while i try to follow your code
i get this error.

Error 1 The type 'System.Xml.XmlReader' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xml, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'. D:\My Work\Projects\Project For Learn\Asp.net MVC\MvcApplication1\MvcApplication1\Controllers\HomeController.cs 23 17 MvcApplication1.
i will appreciate your help.
Left by mohamed attia on Mar 04, 2014 1:26 AM

Your comment:
 (will show your gravatar)


Copyright © Guilherme Cardoso | Powered by: GeeksWithBlogs.net