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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:42:48+00:00 2026-06-18T21:42:48+00:00

I have xml code that would be something like this: file named: player.xml <root>

  • 0

I have xml code that would be something like this:
file named: player.xml

<root>
  <person>
    <fname>Dwight</fname>
    <lname>Howard</lname>
    <vertLeap>
      <try1>32.33</try1>
      <try2>33.33</try2>
      <try3>34.33</try3> 
    </vertLeap>
  </person>
  <person>
    <fname></fname>
    <lname>Jordan</lname>
    <vertLeap>
      <try1>40.33</try1> 
    </vertLeap>
  </person>
</root>

This isn’t my real xml, but should work for the example.
Now I want to use linq to xml to read the data. Iam am trying like this.

Class:

public class Player
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Leap1 { get; set; }
    public int Leap2 { get; set; }
    public int Leap3 { get; set; }

    public WritePlayertoDatabase()
    {
        //do stuff to write
    }
}

Query:

XDocument xDoc = XDocument.Load("player.xml");
var player_query = from p xDoc.Desendants("person")
                   select new Player
                   {
                       FirstName = p.Element("fname"),
                       LastName = p.Element("lname"),
                       Leap1 = p.Element("try1"),
                       Leap2 = p.Element("try2"),
                       Leap3 = p.Element("try3")
                   };

I’m getting a NullReferenceException. Is there a way to test if the elements exist before I try to use the value? Or is there a way better way to accomplish 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-18T21:42:50+00:00Added an answer on June 18, 2026 at 9:42 pm

    So there are a few things wrong with your linq query.

    1) p.Element("fname") will return the fname XML Element, not a string. So you still need to get the element’s value. Similarly, the Leap1-3 properties are int, but you will get the element value as a stirng and need to convert it. But, try1-3 are not ints in the xml, so you probably want to change the type to somehting else in the Player class.

    2) try1 – tryx element s are all children of ‘vertleap’. You can’t directly get element ‘try1’ from ‘person’. It will be null.

    So how about something more like this:

        var player_query = from p in xDoc.Descendants("person")
                           select new Player
                           {
                               FirstName =  p.Element("fname") != null ? p.Element("fname").Value : "",
                               LastName = p.Element("lname") != null ? p.Element("lname").Value : "",
                               Leap1 = p.Element("vertLeap") != null ? (p.Element("vertLeap").Element("try1") != null ? Decimal.Parse(p.Element("vertLeap").Element("try1").Value) : 0) : 0,
    
                               Leap2 = p.Element("vertLeap") != null ? (p.Element("vertLeap").Element("try2") != null ? Decimal.Parse(p.Element("vertLeap").Element("try2").Value) : 0) : 0,
                               Leap3 = p.Element("vertLeap") != null ? (p.Element("vertLeap").Element("try3") != null ? Decimal.Parse(p.Element("vertLeap").Element("try3").Value) : 0) : 0,
                           };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that reads from XML file. It gets five strings (groupId,
I have this test code that just saves an XML file to a folder.
I have a local sqlite database file that looks something like this… _ID |
I have some code that uses Open XML to open up a .docx file,
In my .aspx.cs I have a code that reads a .xml file and I
In my layout xml file I have several buttons like this which share the
I would like to have something like this be generated from hbm2ddl: ______________ ______________
I have an xml file that I download from a url. I would then
I have written some code that loads an XML document using an XmlDocument object
I have the following code that serializes a DataTable to XML. StringWriter sw =

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.