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.
- Click FILE, New, then Project or press CTRL + SHIFT + N.
- 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.
- 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:
- Right click on the Models Folder.
- Click on Add, then Class.
- 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,
- Right Click on Controllers Folder.
- Click Add, then Controller.
- Now one dialog box will open for Add Scaffolding. Select MVC 5 Controller - Empty.
- Now give the name to the Controller as RSSFeedController.
Step 6: Write the following code in RSSFeedController.
- using ReadRSSFeed.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Web;
- using System.Web.Mvc;
- using System.Xml.Linq;
-
- namespace ReadRSSFeed.Controllers
- {
- public class RSSFeedController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
- [HttpPost]
- public ActionResult Index(string RSSURL)
- {
- WebClient wclient = new WebClient();
- string RSSData=wclient.DownloadString(RSSURL);
-
- XDocument xml = XDocument.Parse(RSSData);
- var RSSFeedData = (from x in xml.Descendants("item")
- select new RSSFeed
- {
- Title = ((string)x.Element("title")),
- Link = ((string)x.Element("link")),
- Description = ((string)x.Element("description")),
- PubDate = ((string)x.Element("pubDate"))
- });
- ViewBag.RSSFeed = RSSFeedData;
- ViewBag.URL = RSSURL;
- return View();
- }
- }
- }
Step 7: Now create an index view for RSS Feed Controller. Here are the steps:
- Extend View folder open you will get RSSFeed Folder.
- Right click on RSSFeed Folder.
- 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:
- @{
- ViewBag.Title = "Index";
- }
- <br />
- <h2>RSS Feed Reader</h2>
- <hr />
- <br />
- @using (Html.BeginForm())
- {
- <input type="URL" name="RSSURL" placeholder="Enter RSS Feed URL Here..." class="form-control" value="@ViewBag.URL" style="min-width:100%" />
- <br />
- <input type="submit" class="btn btn-danger" />
- }
- <table class="table table-hover">
- <thead>
- <tr>
- <th>Title</th>
- <th>Link</th>
- <th>Description</th>
- <th>Publish Date</th>
- </tr>
- </thead>
- <tbody>
- @if (ViewBag.RSSFeed != null)
- {
- foreach (var item in ViewBag.RSSFeed)
- {
- <tr>
- <td>@item.Title</td>
- <td><a href="@item.Link">@item.Link</a></td>
- <td>@item.Description</td>
- <td>@item.PubDate</td>
- </tr>
- }
- }
- </tbody>
- </table>
Step 9: Now run the project
Output :
Read more articles on MVC: