Mark W
Mark W

Reputation: 811

Problem with linq to xml

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?

Upvotes: 0

Views: 177

Answers (2)

Jeff Yates
Jeff Yates

Reputation: 62367

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
                   };

Upvotes: 0

Jorge Ferreira
Jorge Ferreira

Reputation: 97839

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
                  };

Upvotes: 1

Related Questions