Search code examples
xmlasp.net-mvcrsslinq-to-xmlviewbag

Simple way to read in an XML for the View (RSS feed)


I have done only some minor XML parsing/reading before, but never within MVC. I only want to grab a small snippet but I am not sure what to use to display this RSS feed.

For example, if the XML looks like this

    <title>Most Recent News</title>
        <link>http://na.leagueoflegends.com/en/rss.xml</link>
        <description></description>
        <language>en-US</language>
          <item>
            <title>Match History Beta Now Live</title>
            <link>http://na.leagueoflegends.com/en/news/game-updates/features/announcing-new-match-history-beta?ref=rss</link>
            <description>lots of long text</description>
            <pubDate>Wed, 04 Jun 2014 14:02:56 +0000</pubDate>
            <dc:creator>sbroome</dc:creator>
            <guid isPermaLink="false">17129 at http://na.leagueoflegends.com</guid>
         </item>
         <item>
           <title>Riot with us at live events!</title>
            <link>http://na.leagueoflegends.com/en/news/community/community-events/riot-us-live-events?ref=rss</link>
            <description>Lots of lon</description>
            <pubDate>Mon, 30 Jun 2014 18:27:13 +0000</pubDate>
            <dc:creator>sbroome</dc:creator>
            <guid isPermaLink="false">18266 at http://na.leagueoflegends.com</guid>
         </item>
       ....dozens more....

I would like something such as the following in ViewBag variables.

<a href="http://na.leagueoflegends.com/en/news/game-updates/features/announcing-new-match-       history-beta?ref=rss">
Match History Beta Now Live</a>
Wed, 04 Jun 2014 14:02:56 +0000

<a href="http://na.leagueoflegends.com/en/news/community/community-events/riot-us-live-events?    ref=rss">
Riot with us at live events!</a>
Mon, 30 Jun 2014 18:27:13 +0000

I have been messing around with random stuff like XDocument and I can store the first item, but nothing else.

XDocument xmlFile = XDocument.Load(@"http://na.leagueoflegends.com/en/rss.xml");

    foreach (XElement element in xmlFile.Descendants("title"))
    {
        String words = element.ToString();
        ViewBag.Ltitle = Regex.Replace(words, "<.*?>", string.Empty);
    }
    foreach (XElement element in xmlFile.Descendants("link"))
    {
        String words = element.ToString();
        ViewBag.Llink = Regex.Replace(words, "<.*?>", string.Empty);
    }
    foreach (XElement element in xmlFile.Descendants("description"))
    {
        String words = element.ToString();
        ViewBag.Ldescription = Regex.Replace(words, "<.*?>", string.Empty);
    }
    foreach (XElement element in xmlFile.Descendants("pubDate"))
    {
        String words = element.ToString();
        ViewBag.LDate = Regex.Replace(words, "<.*?>", string.Empty);
    }

which get's the data and parses it, but I am not sure how to keep multiple items. I would only like to put up ViewBag data for three most recently posted news articles and then call them where need be.

Example of want I want to do

@ViewBag.Ltitle_1<br />
<a href="@ViewBag.Llink_1">@ViewBag.Ldescription_1</a><br />                      
@ViewBag.LDate_1

@ViewBag.Ltitle_2<br />
<a href="@ViewBag.Llink_2">@ViewBag.Ldescription_2</a><br />                      
@ViewBag.LDate_2

@ViewBag.Ltitle_3<br />
<a href="@ViewBag.Llink_3">@ViewBag.Ldescription_3</a><br />                      
@ViewBag.LDate_3

For references the link of this current RSS is: http://na.leagueoflegends.com/en/rss.xml

Thank you!


Solution

  • Figured out a solution in case anyone else needs help.

    Controller

        XDocument xmlFile = XDocument.Load(@"http://na.leagueoflegends.com/en/rss.xml");
    
    
        var LoLtitles = from service in xmlFile.Descendants("item")
                select (string)service.Element("title");
        var LoLlinks = from service in xmlFile.Descendants("item")
                 select (string)service.Element("link");
        var LoLdescriptions = from service in xmlFile.Descendants("item")
                 select (string)service.Element("description");
        var LoLDates = from service in xmlFile.Descendants("item")
                  select (string)service.Element("pubDate");
    
        ViewBag.titles = LoLtitles.ToArray();
        ViewBag.links = LoLlinks.ToArray();
        ViewBag.descriptions = LoLdescriptions.ToArray();
        ViewBag.dates = LoLDates.ToArray();
    

    View

    @ViewBag.titles[0]<br />
    <a href="@ViewBag.links[0]">@ViewBag.descriptions[0]</a><br />
    @ViewBag.dates[0]<br />
    
    @ViewBag.titles[1]<br />
    <a href="@ViewBag.links[1]">@ViewBag.descriptions[1]</a><br />
    @ViewBag.dates[1]<br />
    
    @ViewBag.titles[2]<br />
    <a href="@ViewBag.links[2]">@ViewBag.descriptions[2]</a><br />
    @ViewBag.dates[2]<br />