2009-07-25 11 views
1

私のアプリケーションでは、Webページに表示されている最新の日付を抽出したいと思います。コンテンツを抽出するには、System.Xml.Linq.XDocumentクラスを使用しています。しかし、私は飼料のpubdateを抽出することができません。ここに私のコードがありますが、それは例外です。ドットネットウィンドウアプリケーションでウェブページから最大日付を抽出する方法

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")); 
+0

を例外とは何ですか? DateTimeの形式は、.NETが期待しているものとは異なる可能性があるため、(DateTime)への直接的なキャストは危険です。 –

+0

最後の行の例外シーケンスには要素がありません。 – banita

答えて

0

:タグとは子孫が存在しないよう

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

は、 "フィード"、何も返していません。

この行に変更して、あなたは右の結果を取得します:

var maxPubDates = (from item in feeddata.Descendants("item") select item); 
+0

これは今作業していただきありがとうございます。 – banita

1

使用

feeddata.Descendants("item") 

代わりの

feeddata.Descendants("Feeds") 
0

URIはどんな<Feeds>の要素が含まれていないので、私は主に推測していますか...?

試してみてください。実際にはライン

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(); 
関連する問題