Consuming RSS in Asp.Mvc 3 using C# and Syndication class


Back to learning
Created: 29/06/2013

Consuming  RSS in Asp.Mvc 3 using C# and Syndication class

Software Used:
Visual Studio 2012
Framework 4.0

Dictionary:
RSS:
Really Simple Syndication
Noticias: "News" in spanish
ultimasNoticias: "Last news" in spanish


In this tutorial we will see how to create simple website in Asp.Mvc and consume some RSS and then write the XML content in our web site.


1) Create some basic structure with one masterpage and one view to print the RSS items.
Then add Controller and Model
My structure:



In my case i will read some News RSS from news website and print them on my web

2) Go to the model and add the fallowing code

Include:

using System.ServiceModel.Syndication;
using System.Xml;

namespace News.Models
{
    public class NoticiasModel
    {
        public List<SyndicationItem> GetUltimasNoticias()
        {
            List<SyndicationItem> lastNews = new List<SyndicationItem>();
            lastNews.AddRange(this.GetItemsFromUrl("http://contenidos.lanacion.com.ar/herramientas/rss/origen=2"));

            lastNews.Sort(delegate(SyndicationItem x, SyndicationItem y) { return y.PublishDate.CompareTo(x.PublishDate); });

            return lastNews;
        }

        private List<SyndicationItem> GetItemsFromUrl(string url)
        {
            List<SyndicationItem> lista = new List<SyndicationItem>();
            XmlReader reader = XmlReader.Create(url);
            SyndicationFeed feed = SyndicationFeed.Load(reader);

            foreach (SyndicationItem item in feed.Items)
            {
                item.Copyright = (feed.Copyright == null) ? new TextSyndicationContent(feed.Generator) : new TextSyndicationContent(feed.Copyright.Text);
                item.Id = (item.Id == null) ? item.Links[0].Uri.ToString() : item.Id;
                lista.Add(item);
            }

            return lista;
        }
    }
}

I have 2 methods, one is my main method that i will call from the controller to obtain all the items from RSS
another one will retrieve from the URL where the RSS is found and will add each item from it to my list that i will return to the main method.

You can see that i used sort function in main method, its just to order my news by Date.

3) Go to the controller
And add this code:

namespace News.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public NoticiasModel noticiasModel = new NoticiasModel();

        public ActionResult Index()
        {
            ViewData["ultimasNoticias"] = noticiasModel.GetLastNews();
            return View();
        }

    }
}

Here we call the Model main method to obtain all the items from consumed RSS and add them to ViewData from

which we will print them in the view later

3) View
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <%
        foreach (SyndicationItem noticia in (List<SyndicationItem>)ViewData["ultimasNoticias"])
        {
        %>
         <a href="<%= noticia.Id  %>"><%= noticia.Title.Text + "<div class='copyright'>" + noticia.Copyright.Text + "</div>" %></a>
        <%
        } 
    %>

</asp:Content>

noticia.id: Represents the URL of the item
and Copyright its the real author of the RSS

Thats all there nothing more to say about.

Result:





You can download the project here: Download this project



Let your comments :) !