Reading multiple RSS feeds using MVC and Linq for XML

I'm really rusty when it comes to coding and have worked hard to find the answer, but I'm on my whits end right now.

My website has a database with stored RSS URLs. For life, I cannot figure out how to scroll and display all the data from RSS feeds.

I can get one channel to show it just fine, but showing all of them, I just don’t know what to do to display all of them. Any help would be greatly appreciated!

Model: RSS.cs

namespace MyWebsite.Models { public class RSS { public string Link { get; set; } public string Title { get; set; } public string Description { get; set; } public string PubDate { get; set; } public string Date { get; set; } } } 

Model: RSSReader.cs

 namespace MyWebsite.Models { public class RSSReader { private static string _URL = ""; public static IEnumerable<RSS> GetRSSFeed() { IEnumerable<RSS> feeds = null; XDocument feedXml = null; XNamespace rss = "http://purl.org/rss/1.0/"; XNamespace dc = "http://purl.org/dc/elements/1.1/"; int count = 0; foreach (var url in BansheeData.GetURL(count)) { _URL = url.url1; feedXml = XDocument.Load(_URL); feeds = from feed in feedXml.Descendants(rss + "item") select new RSS { Title = feed.Element(rss + "title").Value, Link = feed.Element(rss + "link").Value, Date = feed.Element(dc + "date").Value.Remove(10) }; count++; } return feeds; } } } 

Controller: RSSController.cs

 public ActionResult Index() { var urlFeed = MyWebsite.Models.RSSReader.GetRSSFeed(); return View(urlFeed); } 

View: _RSSFeed.cshtml

 @model IEnumerable<MyWebsite.Models.RSS> @foreach (var item in Model) { <section> <header class="overflow"> <h1> <a href="@item.Link" target="_blank" class="alt overflow"> <span class="fl">@Html.Raw(item.Title)</span> <span class="ref fr">Source, @Html.Encode(item.Date)</span> </a> </h1> </header> </section> } 
+4
source share
1 answer

I think I found problems in your code. You are not enough to add items to the list for each iteration.

So, instead of:

  foreach (var url in BansheeData.GetURL(count)) { _URL = url.url1; feedXml = XDocument.Load(_URL); feeds = from feed in feedXml.Descendants(rss + "item") select new RSS { Title = feed.Element(rss + "title").Value, Link = feed.Element(rss + "link").Value, Date = feed.Element(dc + "date").Value.Remove(10) }; count++; } return feeds; 

You are trying to do an AddRange in the list for each iteration:

  var rssList = new List<RSS>; foreach (var url in BansheeData.GetURL(count)) { _URL = url.url1; feedXml = XDocument.Load(_URL); feeds = from feed in feedXml.Descendants(rss + "item") select new RSS { Title = feed.Element(rss + "title").Value, Link = feed.Element(rss + "link").Value, Date = feed.Element(dc + "date").Value.Remove(10) }; rssList.AddRange(feeds); count++; } return rssList; 

This should help. You should now get the entire RSS list. The basic logic is when you repeat and get the items that you need in each iteration loop that you need to remember. This means that they are saved in the pace list.

+2
source

All Articles