How to read an RSS and display the information in ASP.NET MVC?

For those who do not know what an RSS is, first we will explain what it is and then we will see the example.


What is an RSS feed and how can I use it?

RSS means Rich Site Summary or also known as Really Simple Syndication. It is a format for publishing web content that changes regularly and is written in XML. There are many creators of free RSS feeds available online, such as Feedburner, Feedity, etc. Using RSS, you can easily publish regularly updated web content, such as blogs, articles, news, etc.

Many sites related to news, weblogs and other online publishers distribute their content as an RSS feed to whoever wishes it. RSS feeds have the following characteristics:

  • Allows you to syndicate the content of your site
  • RSS files can be updated automatically
  • Defines an easy way to share and see headlines and content
  • Allows customized views for different sites

Show an RSS feed in ASP.Net


@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>rss</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
    <style type="text/css">
        .jobs-well {
            padding: 20px;
            margin: 20px 0;
            border: 1px solid #eee;
            border-left-width: 5px;
            border-radius: 3px;
            border-left-color: #0288d1;
        }

            .jobs-well h4 {
                color: #0288d1;
            }
    </style>
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-12 ">
                <br />
                <form action="~/Demo/rss" method="post">
                    <div class="panel panel-primary">
                        <div class="panel-heading">
                            <h4>How to Read and Display RSS Feed in ASP.NET MVC C#</h4>
                        </div>

                        <div class="panel-body">
                            <div class="form-group">
                                <label>Feed URL</label>
                                <input name="txt_url" type="url" class="form-control" placeholder="Feed URL" required value="@ViewBag.txt_url" />
                            </div>
                            <div class="form-group">
                                <label>Feed Type</label>
                                <select name="drp_typ" class="form-control">
                                    <option value="RSS">RSS</option>
                                    <option value="OTHER">OTHER</option>
                                </select>
                            </div>
                        </div>

                        <div class="panel-footer">
                            <button type="submit" class="btn btn-success">Go!</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="col-md-12">
                <div class="panel panel-warning">
                    <div class="panel-heading">
                        <h4>Feed Details </h4>
                    </div>

                    <div class="panel-body">
                        
                        @if (ViewBag.RSS != null)
                        {
                            foreach (var item in ViewBag.RSS)
                            {
                            <div class="jobs-well">
                                <h4>@item.Title></h4>
                                <div class="cnt" style="overflow: hidden">@item.Content</div>

                                <hr />
                                <a href='@item.Link' target="_blank" class="btn btn-success"><i class="fa fa-check"></i>View Details</a>
                            </div>
                            }
                        }
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Code C#

public ActionResult rss(string drp_typ, string txt_url)
        {
            ViewBag.txt_url = txt_url;
            FeedParserHelper parser = new FeedParserHelper();

            if (drp_typ == "OTHER")
            {
            }
            else if (drp_typ == "RSS")
            {
                ViewBag.RSS = parser.Parse(txt_url, FeedType.RSS);
            }
            return View();
        }

Clase FeedParserHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
/// <summary>
    /// Summary description for FeedParserHelper
    /// </summary>
    public class FeedParserHelper
    {
        /// <summary>
        /// Parses the given <see cref="FeedType"/> and returns a <see cref="IList&amp;lt;Item&amp;gt;"/>.
        /// </summary>
        /// <returns></returns>
        public IList<Item> Parse(string url, FeedType feedType)
        {
            return ParseRss(url);
        }

        /// <summary>
        /// Parses an RSS feed and returns a <see cref="IList&amp;lt;Item&amp;gt;"/>.
        /// </summary>
        public virtual IList<Item> ParseRss(string url)
        {
            try
            {
                XDocument doc = XDocument.Load(url);
                // RSS/Channel/item
                var entries = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i => i.Name.LocalName == "item")
                              select new Item
                              {
                                  FeedType = FeedType.RSS,
                                  Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
                                  Link = item.Elements().First(i => i.Name.LocalName == "link").Value,
                                  PublishDate = ParseDate(item.Elements().First(i => i.Name.LocalName == "pubDate").Value),
                                  Title = item.Elements().First(i => i.Name.LocalName == "title").Value
                              };
                return entries.ToList();
            }
            catch
            {
                return new List<Item>();
            }
        }

        private DateTime ParseDate(string date)
        {
            DateTime result;
            if (DateTime.TryParse(date, out result))
                return result;
            else
                return DateTime.MinValue;
        }
    }

    public enum FeedType
    {
        /// <summary>
        /// Really Simple Syndication format.
        /// </summary>
        RSS
    }

    public class Item
    {
        public string Link { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public DateTime PublishDate { get; set; }
        public FeedType FeedType { get; set; }

        public Item()
        {
            Link = string.Empty;
            Title = string.Empty;
            Content = string.Empty;
            PublishDate = DateTime.Today;
            FeedType = FeedType.RSS;
        }
    }

 

Adalberto Cisneros

Desarrollador para plataformas web y móviles. Tengo más de 4 años de desarrollar proyectos independientes, con frecuencia escribo artículos sobre temas de tendencias de desarrollo, diseño y medios de comunicación social

Artículos relacionados
Cómo paginar una tabla html con ASPNET MVC, ajax, jquery y javascript
Cómo paginar una tabla html con ASPNET MVC, ajax, jquery y javascript

En este video aprender&aacute;s a paginar un l

Cómo eliminar registros de un GridView usando ASP.Net Ajax y jQuery
Cómo eliminar registros de un GridView usando ASP.Net Ajax y jQuery

En este artículo te mostrare como eliminar fil

Cómo hacer referencia a los controles de una MasterPage en ASP.NET
Cómo hacer referencia a los controles de una MasterPage en ASP.NET

En este artículo veremos cómo hacer referencia

0 Comentarios
Enviame un comentario