Fetch RSS Feed Content From LINQ To XML

In this article we are going to fetch content from LINQ to XML classes and manipulate XML atom feed and get their elements.

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 or by 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.   
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.         <div>  
  10.             <strong>RSS Feed:</strong><br />  
  11.             <asp:TextBox ID="txtrss" runat="server" TextMode="Url" Width="500px" placeholder="Enter RSS URL"></asp:TextBox>  
  12.             <br />  
  13.             <br />  
  14.             <asp:Button ID="btngetrss" runat="server" Text="Get RSS Feed" OnClick="btngetrss_Click" />  
  15.         </div>  
  16.   
  17.     </form>  
  18. </body>  
  19. </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.   
  6. public class RssNews  
  7. {  
  8.     public string tit   
  9.   {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string desc  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public string auth  
  19.     {  
  20.         get;  
  21.         set;  
  22.     }  
  23.     public string contentlink  
  24.     {  
  25.         get;  
  26.         set;  
  27.     }  
  28.     public string date   
  29.     {  
  30.         get;  
  31.         set;  
  32.     }  
  33.   
  34.   
  35. }  
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.   
  4.        var posts = GetFeeds(txtrss.Text);  
  5.        StringBuilder sb = new StringBuilder();  
  6.        sb.Append("<p style='font-weight:larger'><b>C# Corner Latest Content Fetch From RSS by Aqib Shehzad</b></p>");  
  7.        foreach (var item in posts)  
  8.        {  
  9.             
  10.            sb.Append("<b>Title: </b>" + item.tit);  
  11.            sb.Append("<br />");  
  12.            sb.Append("<b>Description: </b>" + item.desc);  
  13.            sb.Append("<br />");  
  14.            sb.Append("<b>Article Link: </b><a target='_blank' href='" + item.contentlink + "'>" +item.contentlink+"</a>");  
  15.            sb.Append("<br />");  
  16.            sb.Append("<b>Published Date: </b>" + item.date);  
  17.            sb.Append("<br />");  
  18.            sb.Append("<b>Author: </b>" + item.auth);  
  19.            sb.Append("<br />");  
  20.            sb.Append("------------------------------------------------------------------------------------------------------------");  
  21.            sb.Append("<br />");  
  22.   
  23.        }  
  24.        HttpContext.Current.Response.Write(sb);  
  25.    }  
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";  
  5.         rssfeedxml = XDocument.Load(url);  
  6.   
  7.   
  8.         StringBuilder rssContent = new StringBuilder();  
  9.   
  10.         var list = (from descendant in rssfeedxml.Descendants("item")  
  11.   
  12.                     //Response.Write(list);  
  13.                     select new RssNews  
  14.                     {  
  15.                         tit = descendant.Element("title").Value,  
  16.                         desc = descendant.Element("description").Value,  
  17.                         contentlink = descendant.Element("link").Value,  
  18.                         date = descendant.Element("pubDate").Value,  
  19.                         auth = descendant.Element("author").Value  
  20.   
  21.                     });  
  22.         return list.ToList();  
  23.     }  
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";    
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
 
 
Hope you find this useful.