-1
我使用LINQ to XML路徑使用LINQ XML屬性拋出NullReferenceException異常
我有一個時間熊搞清楚爲什麼我下面的LINQ選擇的MyUrl部分獲得的NullReferenceException。我錯過了什麼?
XElement xmlRssItems = XElement.Parse(e.Result);
podlist.ItemsSource = from rssItem in xmlRssItems.Element("channel").Elements("item")
where rssItem.Element("enclosure") != null
select new PodcastItem
{
Title = (string)rssItem.Element("title"),
MyUrl = new Uri(rssItem.Element("enclosure").Attribute("url").Value)
};
...
public class PodcastItem
{
public string Title { get; set; }
public Uri MyUrl { get; set; }
}
XML:
<rss>
<channel>
<item>
<guid isPermaLink="false">tag:blogger.com,1999:blog-811823720557864449.post-3786706865099847612</guid>
<pubDate>Sun, 20 Feb 2011 02:08:00 +0000</pubDate>
<title>#43 - StarCast: "Mork's Homecoming!"</title>
<link>http://starcastshow.blogspot.com/2011/02/43-starcast-morks-homecoming.html</link>
<author>[email protected] (Garrett Weinzierl & Kyle Fergusson)</author>
<thr:total>1</thr:total>
<enclosure url="http://feedproxy.google.com/~r/starcast_rss/~5/Xf0YXRRMrPU/episode_43.mp3" length="0" type="audio/mpeg" />
<feedburner:origEnclosureLink>http://thestarcast.com/shows/starcast/episode_43.mp3</feedburner:origEnclosureLink>
</item>
問題: * 的問題是,RSS feed中有一個節點未有一個外殼項目。我添加了一個簡單的檢查NULL和它的工作完美。感謝聖靈檢查。 *