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 8335451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:40:53+00:00 2026-06-09T03:40:53+00:00

I’ve recently started with Linq, and I’m currently trying to use it to parse

  • 0

I’ve recently started with Linq, and I’m currently trying to use it to parse a heavily nested XML file (which I have no control over). However, I get the “Object reference not set to an instance of an object” error when trying to run the below statement.

The problem is in the “City” line. The data for the city property comes from the xml structure prospect/contactinfo/City/Answer.

But since the field is not mandatory, sometimes the XML will NOT have the city/answer nodes. So I get the error because the “city” node does not exist, and I’m trying to call “.Element()” on it. I’ve found plenty of solutions for this problem, when it is only one node-level down (i.e. if the data I needed was in city, and city was the only node missing).

But when it is two levels down (I.e. trying to get the childnode of a nonexisting node), I have not been able to find any solution.

Hope the question is expressed clearly enough.

Best regards,
Morten

            var prospects = (from prospect in xdoc.Descendants("PROSPECT")
                        select new Prospect {
                            ProspectID = (string) prospect.Element("PROSPECTINFO").Element("PROSPECT_ID"),
                            Name = (string) prospect.Element("PERSONALINFO").Element("FIRSTNAME")+ " " + prospect.Element("PERSONALINFO").Element("SURNAME"),
                            address = (string) prospect.Element("CONTACTINFO").Element("ADDRESSLINE1").Element("ANSWER"),
                            zipCode = (string)prospect.Element("CONTACTINFO").Element("POSTALCODE").Element("ANSWER").Value,
                            City = (string) prospect.Element("CONTACTINFO").Element("CITY").Element("ANSWER"),                                
                        }).ToList();
  • 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-09T03:40:54+00:00Added an answer on June 9, 2026 at 3:40 am

    One option is to use Elements instead of Element. That’s an extension method which finds all elements (optionally with a given name) within either a single element or in a collection of elements. So if you use that repeatedly, you’ll end up with a collection of 0 elements at the end if there aren’t any matches. Use FirstOrDefault to get that element or null, and then the string conversion will do what you want:

    // Still use Element for CONTACTINFO as presumably that's a required element
    City = (string) prospect.Element("CONTACTINFO")
                            .Elements("CITY")
                            .Elements("ANSWER")
                            .FirstOrDefault()
    

    That way you don’t have to write any conditional code – it all just drops out.

    The biggest advantage of this over the sort of check that ChrisF suggests is if the navigation path becomes long. Imagine there are 6 parts of the path, and every one of them is option – you’d need 5 checks (first “a”, then “a.b”, then “a.b.c” etc) followed by the real “fetch the thing”, whereas in this scheme you just add one extra Elements call for each new navigation link.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from

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.