无法在 LINQ to XML 中选择,错误:对象引用未设置到对象的实例
我是使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您在任何地方都使用
Value
属性 - 如果foo.Element(...)
由于元素被丢失的。幸运的是,有一个简单的方法可以解决这个问题 - 转换为字符串。如果“输入”为 null,则XElement
显式转换为string
将返回 null,这非常方便。如果或
元素没有
、
、< code>
子项。但是,如果此失败实际上意味着数据一开始就无效,那么您可能无论如何都需要异常。这取决于您一开始对数据的依赖程度。
The problem is that you're using the
Value
property everywhere - that will always fail if the result offoo.Element(...)
returns null due to the element being missing. Fortunately, there's a simple way round it - cast to string instead. TheXElement
explicit conversion tostring
returns null if the "input" is null, which is very handy.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.
看起来 xml 数据中的
VEHICLES
节点之一没有Vin
、VehicleType
或Year
尝试
或者类似的东西
Looks like one of the
VEHICLES
nodes in your xml data doesn't have aVin
,VehicleType
orYear
Try
or something along those lines