无法在 LINQ to XML 中选择,错误:对象引用未设置到对象的实例

发布于 2024-12-02 10:37:18 字数 1064 浏览 1 评论 0原文

我是使用 Linq to XML 的新手,并且遇到了一个相当麻烦的错误。当尝试提取 XML 文件时,我收到一条错误消息“未将对象引用设置为对象的实例”。它说错误是因为我试图使用 select new 语句。我在下面附加了我的代码:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Xml.Linq;
 public partial class _Default : System.Web.UI.Page
 {
 protected void Page_Load(object sender, EventArgs e)
 {
     XDocument feed = XDocument.Load(Server.MapPath("VEHICLES.XML"));
     var query = from c in feed.Descendants("VEHICLES")
                 where (string) c.Element("VehicleType").Value == "0"
                 select new
                 {
                     Vin = c.Element("Vin").Value,
                     Status = c.Element("VehicleType").Value,
                     Year = c.Element("Year").Value
                 };

    CarLister.DataSource = query;
    CarLister.DataBind();
  }
}

该代码可以很好地使用 select c 拉入所有节点;而不是 selectnew,但理想情况下我只想选择某些信息,并且在拉入它们时需要修改一些条目。我不太确定是什么导致了这个问题,所以关于如何处理的任何指示或想法修复它将不胜感激,如果您需要任何其他信息,请询问!

I am new at using Linq to XML and have run across a rather troublesome error. When trying to pull in my XML file I get an error that reads "Object reference not set to an instance of an object." and it is saying the error is because I am trying to use the select new statement. I have attached my code below:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Xml.Linq;
 public partial class _Default : System.Web.UI.Page
 {
 protected void Page_Load(object sender, EventArgs e)
 {
     XDocument feed = XDocument.Load(Server.MapPath("VEHICLES.XML"));
     var query = from c in feed.Descendants("VEHICLES")
                 where (string) c.Element("VehicleType").Value == "0"
                 select new
                 {
                     Vin = c.Element("Vin").Value,
                     Status = c.Element("VehicleType").Value,
                     Year = c.Element("Year").Value
                 };

    CarLister.DataSource = query;
    CarLister.DataBind();
  }
}

the code works fine pulling in the all of the nodes using select c; instead of selectnew, but ideally I would like to only select certain pieces of information and there are a few entries that I would need to modify when pulling them in. I am not really sure what is causing this issue so any pointers or ideas on how to fix it would be greatly Appreciated, if you need any other info just ask!

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

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

发布评论

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

评论(2

居里长安 2024-12-09 10:37:18

问题是您在任何地方都使用 Value 属性 - 如果 foo.Element(...) 由于元素被丢失的。幸运的是,有一个简单的方法可以解决这个问题 - 转换为字符串。如果“输入”为 null,则 XElement 显式转换为 string 将返回 null,这非常方便。

var query = from c in feed.Descendants("VEHICLES")
            where (string) c.Element("VehicleType") == "0"
            select new
            {
                Vin = (string) c.Element("Vin"),
                Status = (string) c.Element("VehicleType"),
                Year = (string) c.Element("Year")
            };

如果 元素没有 、< code> 子项。

但是,如果此失败实际上意味着数据一开始就无效,那么您可能无论如何都需要异常。这取决于您一开始对数据的依赖程度。

The problem is that you're using the Value property everywhere - that will always fail if the result of foo.Element(...) returns null due to the element being missing. Fortunately, there's a simple way round it - cast to string instead. The XElement explicit conversion to string returns null if the "input" is null, which is very handy.

var query = from c in feed.Descendants("VEHICLES")
            where (string) c.Element("VehicleType") == "0"
            select new
            {
                Vin = (string) c.Element("Vin"),
                Status = (string) c.Element("VehicleType"),
                Year = (string) c.Element("Year")
            };

That will survive in the case that a <VEHICLES> element doesn't have a <VehicleType>, <Vin>, <Status> or <Year> child.

However, if this failure actually means the data was invalid to start with, you may want an exception anyway. It depends on how much you're relying on the data to be sane to start with.

把人绕傻吧 2024-12-09 10:37:18

看起来 xml 数据中的 VEHICLES 节点之一没有 VinVehicleTypeYear

尝试

select new
{
    Vin = c.Element("Vin") == null ? "" : e.Element("Vin").Value,
    Status = c.Element("VehicleType") == null ? "" : e.Element("VehicleType").Value,
    Year = c.Element("Year") == null ? "" : e.Element("Year").Value
};

或者类似的东西

Looks like one of the VEHICLES nodes in your xml data doesn't have a Vin, VehicleType or Year

Try

select new
{
    Vin = c.Element("Vin") == null ? "" : e.Element("Vin").Value,
    Status = c.Element("VehicleType") == null ? "" : e.Element("VehicleType").Value,
    Year = c.Element("Year") == null ? "" : e.Element("Year").Value
};

or something along those lines

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