Donald
Donald

Reputation: 1

RSS Reader C# for multiple URLs

Can't fint the right way to build an RSS Reader for multiple URLs in C#.

namespace RSSFeedApp.Controllers
{
    public static class RSSController
    {
        private static string _blogURL = "http://www.vg.no/export/Alle/rdf.hbs?kat=sport";
        public static IEnumerable<RSSFeedApp.Models.Rss> GetRssFeed()
        {
            XDocument feedXml = XDocument.Load(_blogURL);
            var feeds = from feed in feedXml.Descendants("item")
                        select new RSSFeedApp.Models.Rss
                        {
                            Title = feed.Element("title").Value,
                            Link = feed.Element("link").Value
                        };
            return feeds;
          }
    }
}


namespace RSSFeedApp.Models
{
    public class Rss
    {
        public string Link { get; set; }
        public string Title { get; set; }
    }
}

@foreach (var item in RSSFeedApp.Controllers.RSSController.GetRssFeed())
                                    {
                                        <tr style="border-bottom: 1px solid #34693a;">
                                            <td>
                                                <a       href="@item.Link">@System.Web.HttpUtility.HtmlDecode(item.Title)</a>
                                        </td>
                                        <td>
                                            <a href="@item.Link" target="_blank"><i class="fa fa-book"></i></a>
                                        </td>
                                        </tr>
                                    }

Someone who knows how to do this? Have to use IEnumerable due to the Razor-syntax. As i understand, XDocument cannot read multiple URLs or lists?

Upvotes: 0

Views: 570

Answers (1)

CSintern
CSintern

Reputation: 17

I've done something similar. Here is a rundown of my code:

(My RSS Model is the same)

RSSReader Model:

public class RsssReader
{

    public static IEnumerable<RSSS> GetRssFeed(string url)
    {
        XDocument xdoc;

            var feeds = from feed in xdoc.Descendants("item")
                        select new RSSS
                        {
                            Title = feed.Element("title").Value,
                            Link = feed.Element("link").Value,

                        };
            return feeds;

        }



    }

    public static IEnumerable<RSSS> GetMultipleFeeds(string searchTerm)
    {
        string urluno = "http://your/first/link";
        string url = "http://your/second/link";
        string url2 = "http://your/second/link";
        string urldos = "http://your/second/link";

        return GetRssFeed(urluno).Union(GetRssFeed(url).Union(GetRssFeed(url2).Union(GetRssFeed(urldos))));
    }
} 

RSSController:

    public class RSSSController : Controller
{
    public ActionResult Index(string searchString)
    {

        return View(Search.Models.RsssReader.GetMultipleFeeds(searchString));


    }

Then my View has this:

<table>
<tr>
    <th>
        Title
    </th>
    <th>
        Link
    </th>

</tr>

    @foreach (var item in Model)
    {
            <tr>
                <td>
                    @item.Title
                </td>
                <td>
                    <a href="@item.Link" target="_blank">@item.Link</a>
                </td>
            </tr>
        }
    }

</table>

Upvotes: 1

Related Questions