Note that there are some explanatory texts on larger screens.

plurals
  1. POPath to XML Attribute using LINQ throwing NullReferenceException
    primarykey
    data
    text
    <p>I'm using LINQ to XML</p> <p>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?</p> <pre><code> 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) }; </code></pre> <p>...</p> <pre><code>public class PodcastItem { public string Title { get; set; } public Uri MyUrl { get; set; } } </code></pre> <p>XML:</p> <pre><code>&lt;rss&gt; &lt;channel&gt; &lt;item&gt; &lt;guid isPermaLink="false"&gt;tag:blogger.com,1999:blog-811823720557864449.post-3786706865099847612&lt;/guid&gt; &lt;pubDate&gt;Sun, 20 Feb 2011 02:08:00 +0000&lt;/pubDate&gt; &lt;title&gt;#43 - StarCast: "Mork's Homecoming!"&lt;/title&gt; &lt;link&gt;http://starcastshow.blogspot.com/2011/02/43-starcast-morks-homecoming.html&lt;/link&gt; &lt;author&gt;starcastshow@gmail.com (Garrett Weinzierl &amp;amp; Kyle Fergusson)&lt;/author&gt; &lt;thr:total&gt;1&lt;/thr:total&gt; &lt;enclosure url="http://feedproxy.google.com/~r/starcast_rss/~5/Xf0YXRRMrPU/episode_43.mp3" length="0" type="audio/mpeg" /&gt; &lt;feedburner:origEnclosureLink&gt;http://thestarcast.com/shows/starcast/episode_43.mp3&lt;/feedburner:origEnclosureLink&gt; &lt;/item&gt; </code></pre> <p><strong>THE ISSUE:</strong> *<em>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.</em>*</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload