Copied to clipboard

Flag this post as spam?

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


  • Robin Hansen 135 posts 368 karma points
    Jun 12, 2012 @ 10:23
    Robin Hansen
    0

    Order external feeds by date

    I'm having some issues with some news feeds from an external rss file - the code below workes okay, however I'll like to tweek it a bit - First of all it's important that I get the feeds ordered by date (newest first) - then I need to show one random feed among top 3 newest feeds - does that make any sence...?

     

    @using System.Xml.XPath;
    @using System.Xml;
    @{
    //Fetch RSS XML
    XmlTextReader udBrudRSS = new XmlTextReader("http://myrss.com/rss");
    //Create new XML document
    XmlDocument doc = new XmlDocument();
    //Load in our remote XML into our XML document
    doc.Load(udBrudRSS);
    //Select our nodes we want with some xPath
    XmlNodeList rssItems = doc.SelectNodes("//item");
    }
    @{
        foreach (XmlNode node in rssItems){
                    <h3>@node["title"].InnerText</h3>
                    <h4>@node["pubDate"].InnerText</h4>
                   @Html.Raw(node["content:encoded"].InnerText)
        }   
    }

  • Theo Paraskevopoulos 33 posts 122 karma points
    Jun 12, 2012 @ 12:28
    Theo Paraskevopoulos
    0

    Hi Robin

    I think that Linq2XML may be the way to go - try this:

    @using System.Xml;
    @using System.Linq;
    @using System.Xml.Linq;
    @{
        Random rnd = new Random();
        XDocument doc = XDocument.Load(@"http://myrss.com/rss");
        XElement[] rssItems = doc
            .Descendants("item")
            .OrderByDescending(x => (DateTime)DateTime.Parse(x.Elements("pubDate").FirstOrDefault().Value))
            .Take(3)
            .OrderBy(r => rnd.Next())
            .ToArray();
    }
    @foreach (XElement node in rssItems)
    {
        <h3>@node.Elements("title").FirstOrDefault().Value</h3>
        <p>@DateTime.Parse(node.Elements("pubDate").FirstOrDefault().Value).ToShortDateString()</p>
    }   

    Cheers
    Theo

     

  • Robin Hansen 135 posts 368 karma points
    Jun 12, 2012 @ 12:54
    Robin Hansen
    0

    Works like a charm...! - however I only needed to display One random feed of top 3 - bu I managed to solve this this way - 

    @{int i = 0;}

    @foreach (XElement node in rssItems)

    {

        int pos = i++;

        if (pos == 0)

        { 

        <h3>@node.Elements("title").FirstOrDefault().Value</h3>

        <p>@DateTime.Parse(node.Elements("pubDate").FirstOrDefault().Value).ToShortDateString()</p>

        }

    }

     

    If there's a better/clever way plz. let me know ~:-]

  • Theo Paraskevopoulos 33 posts 122 karma points
    Jun 12, 2012 @ 14:28
    Theo Paraskevopoulos
    0

    You could just call Take(1) again after randomising:

    XElement[] rssItems = doc
    .Descendants("item")
    .OrderByDescending(x => (DateTime)DateTime.Parse(x.Elements("pubDate").FirstOrDefault().Value))
    .Take(3)
    .OrderBy(r => rnd.Next())
    .Take(1)
    .ToArray();

    Cheers
    Theo

  • Robin Hansen 135 posts 368 karma points
    Jun 12, 2012 @ 15:37
    Robin Hansen
    0

    Allright... - sounds good enough to me... ~:-]

Please Sign in or register to post replies

Write your reply to:

Draft