tags:

views:

35

answers:

2

Hello - I am probably missing something obvious, but I am getting a 'Object reference not set to an instance of an object' null error in my Linq to xml query.

Here is a sample of the xml

<airport>
  <station>
  <city>Rutland</city>
  <state>VT</state>
  <country>US</country>
  <icao>KRUT</icao>
  <lat>43.52999878</lat>
  <lon>-72.94999695</lon>
  </station>
</airport>

and here is my query

XDocument geoLocation = XDocument.Load("myTestGeo.xml");

            var currLocation = from geo in geoLocation.Descendants("airport")
                              select new
                              {
                                  City = geo.Element("city").Value,
                                  State = geo.Element("state").Value,
                                  Country = geo.Element("country").Value,
                                  Station = geo.Element("icao").Value
                                  Lat = geo.Element("lat").Value,
                                  Lon = geo.Element("lon").Value
                              };

I have been looking at this all day and tried lots of things, but no luck. Can someone help this dense programmer?

+1  A: 

city and all the other values are inside station and are not direct descendants of airport.

Perhaps some indentation sheds some light into the issue.

<airport>
  <station>
    <city>Rutland</city>
    <state>VT</state>
    <country>US</country>
    <icao>KRUT</icao>
    <lat>43.52999878</lat>
    <lon>-72.94999695</lon>
  </station>
</airport>

This would probably work:

XDocument geoLocation = XDocument.Load("myTestGeo.xml");

var currLocation = from geo in geoLocation.Descendants("station")
                  select new
                  {
                      City = geo.Element("city").Value,
                      State = geo.Element("state").Value,
                      Country = geo.Element("country").Value,
                      Station = geo.Element("icao").Value
                      Lat = geo.Element("lat").Value,
                      Lon = geo.Element("lon").Value
                  };
smink
I see that, but if I query on station I get the same null error.
Mark W
I do not have .net framework with linq so I cannot test for myself. See http://www.linqhelp.com/linq-tutorials/linq-to-xml-displaying-and-filtering-data-from-xml-file-in-c/ for and example that is very similar to what you are trying to achieve.
smink
And are you sure that `myTestGeo.xml` has the XML that you pointed out on the example?
smink
It is a xml file from weather underground, and there are other nodes below this one, but I have used linq to xml to read xml files before this. The nodes below the airport mode are <station></station> also but I am not interested those. If you want I will post a more complete fragment of the file.
Mark W
A: 

Descendants() gives all elements at any level below the current node whereas Element() only looks at direct children of the current node. As all the values you request with the Element() call are children of station and not airport, the calls to Element() return no objects. Dereferencing them with .Value leads to the exception.

If you change your query to the following, it should work:

XDocument geoLocation = XDocument.Load("myTestGeo.xml");

var currLocation = from geo in geoLocation.Descendants("station")
                   select new
                   {
                       City = geo.Element("city").Value,
                       State = geo.Element("state").Value,
                       Country = geo.Element("country").Value,
                       Station = geo.Element("icao").Value
                       Lat = geo.Element("lat").Value,
                       Lon = geo.Element("lon").Value
                   };
Jeff Yates