Read RSS Feed Using MVC

Introduction
 
In this article we will see how to read RSS feed using MVC.

Let us start step-by -:

Step 1: Firstly, we create an MVC Application. To create new MVC Application follow the below given steps.
  1. Click FILENew, then Project or press CTRL + SHIFT + N.



  2. Then one DialogBox will open for new project, go to Installed, Templates, Visual C#, then Web and select ASP .NET Web Application and give name to the project (like ReadRSSFeed) and click on OK button.



  3. After clicking on OK button one more dialog box will open from that dialog box. Select MVC as a project template, then Authentication to Individual User Accounts and then press OK.


Step 2: After creating project your complete project file will be as follows in the Solution Explorer.

 

Step 3: Now add a model class using the following steps:
  1. Right click on the Models Folder.
  2. Click on Add, then Class.


  3. Give the class name as RSSFeed.



Step 4: Write the following code in RSSFeed.cs:

 
Step 5: Now add Empty RSSFeedController to your project. To Add RSSFeedController you can follow the below steps, 
  1. Right Click on Controllers Folder.
  2. Click Add, then Controller.


  3. Now one dialog box will open for Add Scaffolding. Select MVC 5 Controller - Empty.



  4. Now give the name to the Controller as RSSFeedController.
Step 6: Write the following code in RSSFeedController.
  1. using ReadRSSFeed.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Web;  
  7. using System.Web.Mvc;  
  8. using System.Xml.Linq;  
  9.   
  10. namespace ReadRSSFeed.Controllers  
  11. {  
  12.     public class RSSFeedController : Controller  
  13.     {  
  14.         public ActionResult Index()  
  15.         {  
  16.             return View();  
  17.         }  
  18.         [HttpPost]  
  19.         public ActionResult Index(string RSSURL)  
  20.         {  
  21.             WebClient wclient = new WebClient();  
  22.             string RSSData=wclient.DownloadString(RSSURL);  
  23.   
  24.             XDocument xml = XDocument.Parse(RSSData);  
  25.             var RSSFeedData = (from x in xml.Descendants("item")  
  26.                              select new RSSFeed  
  27.                              {  
  28.                                  Title = ((string)x.Element("title")),  
  29.                                  Link = ((string)x.Element("link")),  
  30.                                  Description = ((string)x.Element("description")),  
  31.                                  PubDate = ((string)x.Element("pubDate"))  
  32.                              });  
  33.             ViewBag.RSSFeed = RSSFeedData;  
  34.             ViewBag.URL = RSSURL;  
  35.             return View();  
  36.         }  
  37.     }  
  38. }  
Step 7: Now create an index view for RSS Feed Controller. Here are the steps:
  1. Extend View folder open you will get RSSFeed Folder.
  2. Right click on RSSFeed Folder.



  3. Now give the name of the view as "Index" and Select Empty Template. Then click on Add Button.


 
Step 8: Write the following code to this created view:
  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4. <br />  
  5. <h2>RSS Feed Reader</h2>  
  6. <hr />  
  7. <br />  
  8. @using (Html.BeginForm())  
  9. {  
  10.     <input type="URL" name="RSSURL" placeholder="Enter RSS Feed URL Here..." class="form-control" value="@ViewBag.URL" style="min-width:100%" />  
  11.     <br />  
  12.     <input type="submit" class="btn btn-danger" />  
  13. }  
  14. <table class="table table-hover">  
  15.     <thead>  
  16.         <tr>  
  17.             <th>Title</th>  
  18.             <th>Link</th>  
  19.             <th>Description</th>  
  20.             <th>Publish Date</th>  
  21.         </tr>  
  22.     </thead>  
  23.     <tbody>  
  24.         @if (ViewBag.RSSFeed != null)  
  25.         {  
  26.             foreach (var item in ViewBag.RSSFeed)  
  27.             {  
  28.                 <tr>  
  29.                     <td>@item.Title</td>  
  30.                     <td><a href="@item.Link">@item.Link</a></td>  
  31.                     <td>@item.Description</td>  
  32.                     <td>@item.PubDate</td>  
  33.                 </tr>  
  34.             }  
  35.         }  
  36.     </tbody>  
  37. </table>  
Step 9: Now run the project
 
Output :

 
 

Read more articles on MVC:


Similar Articles