2011-03-02 61 views
-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 &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 feed中有一個節點未有一個外殼項目。我添加了一個簡單的檢查NULL和它的工作完美。感謝聖靈檢查。 *

回答

2

您是否編輯過rss標籤以刪除名稱空間?這工作對我很好,請注意「的xmlns:THR」和「的xmlns:Feedburner的」在RSS元素:

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 &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> 
     </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);