views:

270

answers:

3

hi

In my application I want to extract the latest date shown in web page. I am using System.Xml.Linq.XDocument class to extract content. But I am not able to extract pubdate of feed. Here is my code but its giving exception.

 System.Xml.Linq.XDocument feeddata = 
   System.Xml.Linq.XDocument.Load(
    "http://feeds2.feedburner.com/plasticsnews/plasticsinformation/plastopedia");
  var maxPubDates = (from feeds in feeddata.Descendants("Feeds")select feeds );
  DateTime maxDate = maxPubDates.Max(feed => (DateTime) feed.Element("pubDate"));
+1  A: 

Use

 feeddata.Descendants("item")

instead of

 feeddata.Descendants("Feeds")
maciejkow
A: 

I'm guessing mainly because the uri doesn't contain any <Feeds> elements...?

Try:

System.Xml.Linq.XDocument feeddata = System.Xml.Linq.XDocument.Load(
    "http://feeds2.feedburner.com/plasticsnews/plasticsinformation/plastopedia");
var maxPubDates = feeddata.Descendants("item").Select(
        item => (DateTime)item.Element("pubDate")).Max();
Marc Gravell
A: 

Actually the line:

var maxPubDates = (from feeds in feeddata.Descendants("Feeds") select feeds);

is not returning anything, as there is no descendant with tag "Feeds".

Change it to this line and you will get right results:

var maxPubDates = (from item in feeddata.Descendants("item") select item);
TheVillageIdiot
thanks its working now .
banita