Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kasper Dyrvig 246 posts 379 karma points
    May 25, 2012 @ 10:26
    Kasper Dyrvig
    0

    Only get first node in xml feed

    I'm new in Razor... so I'm puzzleing a lot with even small features.

    I have succesfully made a macro that renders a rss feed (thanks to Our). Now I need it to only render the first item. How do I do that?

  • Kasper Dyrvig 246 posts 379 karma points
    May 29, 2012 @ 08:31
    Kasper Dyrvig
    0

    Well, I got it:

     

    @{
    XmlTextReader dibertFeed = new XmlTextReader("http://feed.dilbert.com/dilbert/daily_strip");
    XmlDocument doc = new XmlDocument();
    doc.Load(dibertFeed);
    XmlNodeList rssItems = doc.SelectNodes("//item");
    }
    @{
      List<dynamic> strips = new List<dynamic>();
      foreach (XmlNode node in rssItems)
    {
          var i = new
          {
              Title = Html.Raw(node["description"].InnerText),
              Url = node["link"].InnerText
          };
          strips.Add(i);
      }
      foreach(var item in strips.Take(1))
      {
          <div class="dilbert"><a href="@item.Url" target="_blank">@item.Title</a></div>
      }
    }

    Thanks to Douglas Ludlow and Warren Buckley 

     

Please Sign in or register to post replies

Write your reply to:

Draft