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

Monday, October 18, 2010 #

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.