使用 LINQ 抛出 NullReferenceException 的 XML 属性路径

发布于 10-19 14:11 字数 1950 浏览 3 评论 0原文

我正在使用 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 &amp; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

闻呓2024-10-26 14:11:34

您是否编辑了 rss 标签以删除名称空间?这对我来说效果很好,请注意 rss 元素中的“xmlns:thr”和“xmlns:feedburner”:

private void buttonTest1_Click(object sender, RoutedEventArgs e)
{
    string sResult = @"<rss xmlns:thr=""http://www.itunes.com/dtds/random.dtd"" xmlns:feedburner=""http://rssnamespace.org/feedburner/ext/1.0"">
        <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>
        </channel>
        </rss>";
    XElement xmlRssItems = XElement.Parse(sResult);
    var result = from rssItem in xmlRssItems.Element("channel").Elements("item")
                            select new PodcastItem
                            {
                                Title = (string)rssItem.Element("title"),
                                MyUrl = new Uri(rssItem.Element("enclosure").Attribute("url").Value)
                            };
    Console.WriteLine("{0}", result);
}

处理它的另一种方法是添加命名空间并使用 XmlReader 加载元素:

XmlNamespaceManager xnm = new XmlNamespaceManager(new NameTable());
xnm.AddNamespace("thr", "");
xnm.AddNamespace("feedburner", "");
XmlParserContext xpc = new XmlParserContext(null, xnm, null, XmlSpace.None);
XmlTextReader xtr = new XmlTextReader(sResult, XmlNodeType.Element, xpc);
XElement xmlRssItems = XElement.Load(xtr);
var result = from rssItem in xmlRssItems.Element("channel").Elements("item")
    select new PodcastItem
    {
        Title = (string)rssItem.Element("title"),
        MyUrl = new Uri(rssItem.Element("enclosure").Attribute("url").Value),
        Total = (string)rssItem.Element("total"),
        OriginalEnclosureLink = (string)rssItem.Element("origEnclosureLink")
    };
Console.WriteLine("{0}", result);

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:

private void buttonTest1_Click(object sender, RoutedEventArgs e)
{
    string sResult = @"<rss xmlns:thr=""http://www.itunes.com/dtds/random.dtd"" xmlns:feedburner=""http://rssnamespace.org/feedburner/ext/1.0"">
        <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>
        </channel>
        </rss>";
    XElement xmlRssItems = XElement.Parse(sResult);
    var result = from rssItem in xmlRssItems.Element("channel").Elements("item")
                            select new PodcastItem
                            {
                                Title = (string)rssItem.Element("title"),
                                MyUrl = new Uri(rssItem.Element("enclosure").Attribute("url").Value)
                            };
    Console.WriteLine("{0}", result);
}

Another way to handle it would be to add the namespaces and use XmlReader to load your element:

XmlNamespaceManager xnm = new XmlNamespaceManager(new NameTable());
xnm.AddNamespace("thr", "");
xnm.AddNamespace("feedburner", "");
XmlParserContext xpc = new XmlParserContext(null, xnm, null, XmlSpace.None);
XmlTextReader xtr = new XmlTextReader(sResult, XmlNodeType.Element, xpc);
XElement xmlRssItems = XElement.Load(xtr);
var result = from rssItem in xmlRssItems.Element("channel").Elements("item")
    select new PodcastItem
    {
        Title = (string)rssItem.Element("title"),
        MyUrl = new Uri(rssItem.Element("enclosure").Attribute("url").Value),
        Total = (string)rssItem.Element("total"),
        OriginalEnclosureLink = (string)rssItem.Element("origEnclosureLink")
    };
Console.WriteLine("{0}", result);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文