Guilherme Cardoso, Blog EN

.NET geek

  Home  |   Contact  |   Syndication    |   Login
  21 Posts | 0 Stories | 21 Comments | 0 Trackbacks

News

Name: Guilherme Cardoso
Age: 19
Country: Portugal


pontonetpt

NetPonto

xaml pt

Twitter





Archives

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

Feedback

# re: ASP.NET MVC - Reading feeds RSS 7/23/2011 9:13 AM Bruno
Obrigado! ;)

# re: ASP.NET MVC - Reading feeds RSS 7/23/2011 9:16 AM Guilherme Cardoso
Why don't you order it with linq?

# re: ASP.NET MVC - Reading feeds RSS 11/25/2011 12:00 AM Fabio
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?

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: