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

  • SEARCH
  • Home
  • 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 8851345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:07:59+00:00 2026-06-14T13:07:59+00:00

I have something similar to the XML below. I’m parsing it using Linq in

  • 0

I have something similar to the XML below. I’m parsing it using Linq in C#, and it works great, until we get to a product which doesn’t have a type.

<productList>
    <product>
        <type>
            <colour>red</colour>
        </type>
        <name>First</name>
    </product>
    <product>
        <name>Second</name>
    </product>
</productList>

I’m trying to access the colour element under type, but when the code reaches a product which doesn’t have the type element, I get the “Object reference not set to an instance of an object” error.

Here’s the code I’m using to access it at the moment. Things have got a little complicated with the attempts I’ve tried to solve this.

productColour = products.Element("type").Descendants().FirstOrDefault() == null ? string.Empty : products.Element("type").Descendants().FirstOrDefault().Value,

I know the colour element is always the first underneath type, so I can use .FirstOrDefault(), but I still get the same error using this code.

Can anyone point me towards the right direction? I’ve also tried casting to a string, and also having: ?? " " at the end, all to no avail so far.

Edit: Thanks to @anthony-pegram, it looks like the issue is that I’m always trying to grab the descendants, even if the parent element doesn’t exist. So it looks like I need to check the parent element before grabbing the child – can anyone point me in a direction for this?

  • 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-06-14T13:08:01+00:00Added an answer on June 14, 2026 at 1:08 pm

    Elements() wiil return an IEnumerable. If the element does not exist, IEnumerable will just be empty and calling Elements again on it will not product a null reference exception. The code below should pull the colour from under a product\type element without throwing a null reference exception. If colour or type do not exist, it will return a null.

    (string)product.Elements("type").Elements("colour").FirstOrDefault()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to output something similar to the following using XSLT XML: <myroot> <node1>
I have something similar to the following in an XML document: <variables> <variable> <name>ctrl_btn</name>
In my XSD, I have something similar to this: <?xml version=1.0 encoding=utf-8 ?> <schema
I have something similar to that: <table> <tr> <td>Hello,<br/>World!</td> </tr> </table> Both lines Hello,
i have something similar to the following... used a python script to JSON to
I am just trying to find out that does puppet have something similar to
I have something very similar to this example: https://developers.google.com/maps/documentation/javascript/examples/directions-simple All I am trying to
I have an ASP.NET web site with something similar to the following code: .aspx
We currently have an ant task that contains something similar to the following: <filelist
i have something that requires a matrix of values, similar to pokemon: (source: firechao.com

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.