开发者

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?


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜