【问题标题】:Problem with linq to xmllinq to xml的问题
【发布时间】:2010-03-06 01:54:38
【问题描述】:

我可能遗漏了一些明显的东西,但在我的 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
                              };

我整天都在看这个并尝试了很多东西,但没有运气。有人可以帮助这个密集的程序员吗?

【问题讨论】:

    标签: c# linq-to-xml


    【解决方案1】:

    city 和所有其他值都在车站内,不是机场的直系后代。

    也许一些缩进可以解释这个问题。

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

    【讨论】:

    • 我看到了,但是如果我在站上查询,我会得到同样的空错误。
    • 我没有带有 linq 的 .net 框架,所以我无法自己测试。请参阅linqhelp.com/linq-tutorials/… 以获取与您要实现的目标非常相似的示例。
    • 您确定myTestGeo.xml 具有您在示例中指出的 XML 吗?
    • 这是一个来自weather underground的xml文件,这个下面还有其他节点,不过我之前用过linq to xml读取xml文件。机场模式下的节点也是 但我不感兴趣。如果你愿意,我会发布一个更完整的文件片段。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多