Copied to clipboard

Flag this post as spam?

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


  • Steve 472 posts 1216 karma points
    Feb 01, 2013 @ 16:41
    Steve
    0

    Sorting a RSS Feed by Date

    I am bringing in an RSS feed using the razor below. I need 2 things though: to be able to sort by a node <event-date> and to only list the 5 most recient items in the feed. Any help would be appreciated. Thanks!

    @using System.Xml.XPath;
    @using System.Xml;
    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{

    XmlTextReader udBrudRSS = new XmlTextReader("http://rosehulman.imodules.com/controls/cms_v2/components/rss/rss.aspx?sid=1554&gid=1&calcid=1110&page_id=402");
    XmlDocument doc = new XmlDocument();
    doc.Load(udBrudRSS);
    XmlNodeList rssItems = doc.SelectNodes("//item");

    }

    <ul class="rss-feed">

        @{

    foreach (XmlNode node in rssItems) {
          <li>
               <div class="date">@node["event-date"].InnerText</div>
               <a href="@node["link"].InnerText">@node["title"].InnerText</a>
         </li> 
            }
     }

    </ul>

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 01, 2013 @ 17:10
    Jan Skovgaard
    0

    Hi Steve

    From the above I'm asuming you're using Macroscript Razor (Not MVC Razor).

    If So I think you should be able to use "OrderBy" as described in this post http://umbraco.com/follow-us/blog-archive/2011/3/1/umbraco-razor-feature-walkthrough-%E2%80%93-part-4.aspx

    Does this help?

    /Jan

  • Steve 472 posts 1216 karma points
    Feb 01, 2013 @ 19:38
    Steve
    0

    I can't seem to use any objects with the "rssItems" as it is is a XmlNodelist variable. Is there another way to do this?

  • Mike Chambers 636 posts 1253 karma points c-trib
    Feb 04, 2013 @ 09:37
    Mike Chambers
    0

    not sure this is possible on an xmlNodeList directly... however as you have you xmlDicument you can use an XPathNavigator to manipulate..

    Something like this...(ps untested and you might have to parse you event-date if it's not UTC format...


               
    XPathNavigator nav = doc.CreateNavigator();
               
    XPathExpression exp = nav.Compile("//item");
                exp
    .AddSort("event-date",XmlSortOrder.Ascending,XmlCaseOrder.None,"",XmlDataType.DateTime);
               
    XPathNodeIterator iter = nav.Select(exp);
               
    while(iter.MoveNext())
               
    {
                   
    string res = iter.Current.Value;
               
    }
  • Steve 472 posts 1216 karma points
    Feb 04, 2013 @ 13:26
    Steve
    0

    Is there a way to limit the number of items coming into the feed?

Please Sign in or register to post replies

Write your reply to:

Draft