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) } }
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...?
Hi Robin
I think that Linq2XML may be the way to go - try this:
Cheers
Theo
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 ~:-]
You could just call Take(1) again after randomising:
Cheers
Theo
Allright... - sounds good enough to me... ~:-]
is working on a reply...