使用 LINQ 抛出 NullReferenceException 的 XML 属性路径
我正在使用 LINQ to XML,
我花了很长时间才弄清楚为什么我在下面的 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 源中存在一个节点没有附件项目。我添加了一个简单的 NULL 检查,它运行得很好。感谢您进行健全性检查。*
I'm using LINQ to XML
I'm having a bear of a time figuring out why I'm getting NullReferenceException on the MyUrl portion of the LINQ selection below. What am I missing?
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>
THE ISSUE:
*THE PROBLEM WAS THAT IN THE RSS FEED THERE WAS ONE NODE THAT DIDN'T HAVE AN ENCLOSURE ITEM. I ADDED A SIMPLE CHECK FOR NULL AND IT WORKS PERFECTLY. THANKS FOR THE SANITY CHECK.*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您是否编辑了 rss 标签以删除名称空间?这对我来说效果很好,请注意 rss 元素中的“xmlns:thr”和“xmlns:feedburner”:
处理它的另一种方法是添加命名空间并使用 XmlReader 加载元素:
Have you edited your rss tag to remove the namespaces? This works fine for me, notice the "xmlns:thr" and "xmlns:feedburner" in the rss element:
Another way to handle it would be to add the namespaces and use XmlReader to load your element: