linq 到 xml 的问题

发布于 2024-08-24 05:04:58 字数 1132 浏览 9 评论 0原文

我可能遗漏了一些明显的东西,但我在 Linq to xml 查询中收到“对象引用未设置到对象实例”空错误。

这是 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>

,这是我的查询,

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热风软妹 2024-08-31 05:04:59

Descendants() 给出当前节点以下任何级别的所有元素,而 Element() 仅查看当前节点的直接子节点。由于您通过 Element() 调用请求的所有值都是 station 的子项,而不是 airport,因此对 Element() 的调用 不返回任何对象。使用 .Value 取消引用它们会导致异常。

如果将查询更改为以下内容,它应该可以工作:

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
                   };
神经暖 2024-08-31 05:04:58

city 和所有其他值都在 station 内,而不是 airport 的直系后代。

也许一些缩进可以让我们了解这个问题。

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

这可能会起作用:

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

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
                  };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文