Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4013092
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:19:42+00:00 2026-05-20T09:19:42+00:00

I’m using LINQ to XML I’m having a bear of a time figuring out

  • 0

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>starcastshow@gmail.com (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>

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.*

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:19:43+00:00Added an answer on May 20, 2026 at 9:19 am

    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>starcastshow@gmail.com (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);
    }
    

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.