Fetch RSS Feed Content From LINQ To XML


In this article I will demonstrate how to build RSS Feed Reader using LINQ to XML.
Step 1

You  need to grab the feed Link. In my case i will be using C# Corner Feed Link. The following image helps to find the desired link from C# Corner Main Page.

 You can get Link from above yellow highlighted regions. I am pasting the links for reference.
Step 2

Now after opening Visual Studio you can creating a new website by going to File, New, then Website orby pressing Shift+Alt+N command in Visual Studio.

Step 3: Create ASP.NET Empty Web Site and then add new web form to the page.

Step 4: Add Controls to the page. In our case we required a TextBox and Button.

Here is the code for building the Rss Reader.

  1. <!DOCTYPE html>
  2. <html xmlns=http://www.w3.org/1999/xhtml&#8221;>
  3. <head runat=“server”>
  4.     <title></title>
  5. </head>
  6. <body>
  7.     <form id=“form1” runat=“server”>
  8.         
  9.             RSS Feed:
  10.             “txtrss” runat=“server” TextMode=“Url” Width=“500px” placeholder=“Enter RSS URL”>
  11.             
  12.             
  13.             “btngetrss” runat=“server” Text=“Get RSS Feed” OnClick=“btngetrss_Click” />
  14.         
  •     </form>
  • </body>
  • </html>
  • Step 5: Add new class and public properties inside it.

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. public class RssNews
    6. {
    7.     public string tit
    8.   {
    9.         get;
    10.         set;
    11.     }
    12.     public string desc
    13.     {
    14.         get;
    15.         set;
    16.     }
    17.     public string auth
    18.     {
    19.         get;
    20.         set;
    21.     }
    22.     public string contentlink
    23.     {
    24.         get;
    25.         set;
    26.     }
    27.     public string date
    28.     {
    29.         get;
    30.         set;
    31.     }
    32. }
    Step 6: Add required Namespaces
    1. using System.Xml;
    2. using System.Xml.Linq;
    Step 7: Now we can write code behind logic in button.
    1. protected void btngetrss_Click(object sender, EventArgs e)
    2.    {
    3.        var posts = GetFeeds(txtrss.Text);
    4.        StringBuilder sb = new StringBuilder();
    5.        sb.Append(“<p style=’font-weight:larger’><b>C# Corner Latest Content Fetch From RSS by Aqib Shehzad</b></p>”);
    6.        foreach (var item in posts)
    7.        {
    8.            sb.Append(“<b>Title: </b>” + item.tit);
    9.            sb.Append(“<br />”);
    10.            sb.Append(“<b>Description: </b>” + item.desc);
    11.            sb.Append(“<br />”);
    12.            sb.Append(“<b>Article Link: </b><a target=’_blank’ href='” + item.contentlink + “‘>” +item.contentlink+“</a>”);
    13.            sb.Append(“<br />”);
    14.            sb.Append(“<b>Published Date: </b>” + item.date);
    15.            sb.Append(“<br />”);
    16.            sb.Append(“<b>Author: </b>” + item.auth);
    17.            sb.Append(“<br />”);
    18.            sb.Append(“————————————————————————————————————“);
    19.            sb.Append(“<br />”);
    20.        }
    21.        HttpContext.Current.Response.Write(sb);
    22.    }

    The Feed Reader method from URL.

    1. public static IEnumerable<RssNews> GetFeeds(string url)
    2.     {
    3.         XDocument rssfeedxml;
    4.         XNamespace namespaceName = http://www.w3.org/2005/Atom&#8221;;
    5.         rssfeedxml = XDocument.Load(url);
    6.         StringBuilder rssContent = new StringBuilder();
    7.         var list = (from descendant in rssfeedxml.Descendants(“item”)
    8.                     //Response.Write(list);
    9.                     select new RssNews
    10.                     {
    11.                         tit = descendant.Element(“title”).Value,
    12.                         desc = descendant.Element(“description”).Value,
    13.                         contentlink = descendant.Element(“link”).Value,
    14.                         date = descendant.Element(“pubDate”).Value,
    15.                         auth = descendant.Element(“author”).Value
    16.                     });
    17.         return list.ToList();
    18.     }

    Now after Initializes a new instance of the XDocument class. we can declare the namespace of Rss Feed.

    1. XNamespace namespaceName = http://www.w3.org/2005/Atom&#8221;;

    Now we can load the URI contents to create the new Xdocument.

    1. XDocument.Load(url);

    after fetching data we can get the descendant element for which we want data.

    1. var list = (from descendant in rssfeedxml.Descendants(“item”)
    and after manipulation we return the data as list and do processing per need.
    Step 8:
    Final Output
     
    Advertisements