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?
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.
This would probably work: