I have been playing around with linking XML files to dropdown lists and gridviews.
我一直在尝试将XML文件链接到下拉列表和gridview。
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?
我成功地从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();
4 个解决方案
#1
6
Avoid using .Value
; a range of null-safe implicit conversion operators are available:
避免使用value;可以使用一系列的null-safe隐式转换操作符:
var q = from c in xmlDoc.Descendants("Images")
where (string)c.Attribute("PropertyId")
== DropDownList1.SelectedValue.ToString()
select new
{
PropertyID = (string)c.Element("ThumbUrl"),
};
#2
1
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.
可以是null,然后调用它们上的. tostring()或. value会给您所看到的异常。
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).
如果您不喜欢通过nullreferenceexception捕获XML问题,那么您需要将属性()调用的值转换为一个局部变量,然后对其进行测试(或者调用它两次并测试第一次对null的调用)。
#3
0
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:
尝试:where 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();
#4
0
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,
};
#1
6
Avoid using .Value
; a range of null-safe implicit conversion operators are available:
避免使用value;可以使用一系列的null-safe隐式转换操作符:
var q = from c in xmlDoc.Descendants("Images")
where (string)c.Attribute("PropertyId")
== DropDownList1.SelectedValue.ToString()
select new
{
PropertyID = (string)c.Element("ThumbUrl"),
};
#2
1
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.
可以是null,然后调用它们上的. tostring()或. value会给您所看到的异常。
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).
如果您不喜欢通过nullreferenceexception捕获XML问题,那么您需要将属性()调用的值转换为一个局部变量,然后对其进行测试(或者调用它两次并测试第一次对null的调用)。
#3
0
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:
尝试:where 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();
#4
0
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,
};