我的 LINQ to XML 代码中出现空引用异常

发布于 2024-11-02 13:29:23 字数 501 浏览 8 评论 0原文

我一直在尝试将 XML 文件链接到下拉列表和网格视图。

我已设法从 XML 文档填充一个下拉列表,然后将 gridview 填充到另一个下拉列表,但当尝试添加 where 子句时,我收到空引用异常,但不确定原因。我该如何解决这个问题?

XDocument xmlDoc = XDocument.Load(Server.MapPath("XMLFile.xml"));
var q = from c in xmlDoc.Descendants("Images")
        where c.Attribute("PropertyId").Value == DropDownList1.SelectedValue.ToString()
        select new
        {
            PropertyID = c.Element("ThumbUrl").Value,
        };
GridView1.DataSource = q;
GridView1.DataBind();

I have been playing around with linking XML files to dropdown lists and gridviews.

I have managed to populate one dropdown list from the XML document and then a gridview to another but when try to add a where clause, I get a null reference exception and not sure why. How can I resolve this?

XDocument xmlDoc = XDocument.Load(Server.MapPath("XMLFile.xml"));
var q = from c in xmlDoc.Descendants("Images")
        where c.Attribute("PropertyId").Value == DropDownList1.SelectedValue.ToString()
        select new
        {
            PropertyID = c.Element("ThumbUrl").Value,
        };
GridView1.DataSource = q;
GridView1.DataBind();

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

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

发布评论

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

评论(4

不一样的天空 2024-11-09 13:29:23

避免使用.Value;可以使用一系列空安全隐式转换运算符:

var q = from c in xmlDoc.Descendants("Images")
        where (string)c.Attribute("PropertyId")
               == DropDownList1.SelectedValue.ToString()
        select new
        {
            PropertyID = (string)c.Element("ThumbUrl"),
        };

Avoid using .Value; a range of null-safe implicit conversion operators are available:

var q = from c in xmlDoc.Descendants("Images")
        where (string)c.Attribute("PropertyId")
               == DropDownList1.SelectedValue.ToString()
        select new
        {
            PropertyID = (string)c.Element("ThumbUrl"),
        };
爱情眠于流年 2024-11-09 13:29:23

其中任何一个:

c.Attribute("PropertyId")
c.Element("ThumbUrl")
DropDownList1.SelectedValue

都可以为 null,然后对它们调用 .ToString() 或 .Value 会给您带来您所看到的异常。

如果您不乐意通过 NullReferenceExceptions 捕获 XML 问题,那么您需要将 Attribute() 调用的值放入局部变量中,然后对其进行测试(或者调用它两次并测试第一次调用是否为 null)。

Any of these:

c.Attribute("PropertyId")
c.Element("ThumbUrl")
DropDownList1.SelectedValue

could be null, and then calling .ToString() or .Value on them would give you the exception you're seeing.

If you're not happy to catch XML problems via NullReferenceExceptions, then you need to take the value of the Attribute() call into a local variable and then test against that (or call it twice and test the first call for null).

活泼老夫 2024-11-09 13:29:23

尝试:
其中 c.Attribute("PropertyId") != null &&条件部分的 c.Attribute("PropertyId").Value == DropDownList1.SelectedValue.ToString()c.Element("ThumbUrl") != null 。您的代码应如下所示:

XDocument xmlDoc = XDocument.Load(Server.MapPath("XMLFile.xml"));
var q = from c in xmlDoc.Descendants("Images")
        where c.Attribute("PropertyId") != null 
        && c.Attribute("PropertyId").Value == DropDownList1.SelectedValue.ToString() 
        && c.Element("ThumbUrl") != null
        select new
        {
            PropertyID = c.Element("ThumbUrl").Value,
        };
GridView1.DataSource = q;
GridView1.DataBind();

Try:
where c.Attribute("PropertyId") != null && c.Attribute("PropertyId").Value == DropDownList1.SelectedValue.ToString() for the condition part and c.Element("ThumbUrl") != null . Your code should look like this:

XDocument xmlDoc = XDocument.Load(Server.MapPath("XMLFile.xml"));
var q = from c in xmlDoc.Descendants("Images")
        where c.Attribute("PropertyId") != null 
        && c.Attribute("PropertyId").Value == DropDownList1.SelectedValue.ToString() 
        && c.Element("ThumbUrl") != null
        select new
        {
            PropertyID = c.Element("ThumbUrl").Value,
        };
GridView1.DataSource = q;
GridView1.DataBind();
旧故 2024-11-09 13:29:23
from x in document.Descendants("Images")
let xElement = x.Element("PropertyId")
where xElement != null && xElement.Value == DropDownList1.SelectedValue.ToString()
select new
{
   PropertyID = c.Element("ThumbUrl").Value,
};
from x in document.Descendants("Images")
let xElement = x.Element("PropertyId")
where xElement != null && xElement.Value == DropDownList1.SelectedValue.ToString()
select new
{
   PropertyID = c.Element("ThumbUrl").Value,
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文