[英]Xelement Linq with Null value?
I try getting all the Xelement value and insert into an array, however when the Xelement is null, it will show the error as below when insert into array: 我尝试获取所有Xelement值并插入到数组中,但是当Xelement为null时,插入到数组中时将显示以下错误:
Object reference not set to an instance of an object.
你调用的对象是空的。
Is it possible to remove the null value? 是否可以删除空值? Or replace the null value with Not null value?
还是将空值替换为非空值? Please advice me.
请给我建议。
My Node in XML format in Database (3rd value is null): 数据库中XML格式的“我的节点”(第三个值为null):
<AnswerData><Answer1>a1</Answer1><Answer2>a2</Answer2><Answer3></Answer3></AnswerData>
My controller: 我的控制器:
int z = 0;
string[] multiresultanswer = new string[choicesubqarray.Count()];
for (int x = 0; x < multiresultanswer.Count(); x++)
{
XElement RateAns =
(from node in qconfig.Elements("Answer" + (x + 1))
select node).SingleOrDefault();
// Error when store 3rd value ====> multiresultanswer[z++] = RateAns.Value;
}
I think it would be easier for you to work this way: 我认为这样工作会更容易:
string[] multiresultanswe = Enumerable
.Range(1, choicesubqarray.Count())
.Select(x => (from node in qconfig.Elements("Answer" + x)
select node).SingleOrDefault())
.Where(x => x != null)
.Select(x => x.Value)
.ToArray();
That would entail getting the range of answers ( Enumerable.Range
), changing the list of numbers to required result ( Select
), leaving out the nulls ( Where
), converting it to the value ( Select
), and putting the results into the array ( ToArray
). 这将需要得到答案的范围(
Enumerable.Range
),将数字列表更改为所需的结果( Select
),省略空值( Where
),将其转换为值( Select
),然后将结果放入数组中( ToArray
)。
You can also improve (from node in qconfig.Elements("Answer" + x) select node).SingleOrDefault()
by changing it into qconfig.Elements("Answer" + x).SingleOrDefault()
which does the same thing. 您也可以通过将其更改为
qconfig.Elements("Answer" + x).SingleOrDefault()
来改进(from node in qconfig.Elements("Answer" + x) select node).SingleOrDefault()
。
You seem to misunderstand the meaning of SingleOrDefault
. 您似乎误解了
SingleOrDefault
的含义。
SingleOrDefault
returns null
if no elements have been found. 如果未找到任何元素,则
SingleOrDefault
返回null
。 Thus, in the case of (RateAns == null)
, there is no answer with the given number in your XML. 因此,在
(RateAns == null)
的情况下,XML中没有给定数字的答案 。 Thus, there is nothing to remove. 因此,没有什么要删除的。
You have to check if the Value
-property of your current XElement
is NULL
. 您必须检查当前
XElement
的Value
property是否为NULL
。
var rateAns = qconfig.Elements("Answer" + (x + 1)).Where(el => el.Value != null));
Thus you won´t need any further handling for NULL
-values, so your RateAns == null
is obsolete because your query does not return any NULL-elements. 因此,您不需要对
NULL
任何进一步的处理,因此RateAns == null
已过时,因为查询不返回任何NULL元素。
string xml = @"<AnswerData><Answer1>a1</Answer1><Answer2>a2</Answer2><Answer3>null</Answer3></AnswerData>";
XElement root = XElement.Parse(xml);
int count = 5;
var query = from n in Enumerable.Range(1, count)
join e in root.Elements() on n.ToString() equals e.Name.LocalName.Substring(6) into g
from e in g.DefaultIfEmpty()
select e == null ? null : e.Value == "null" ? null : e.Value;
foreach (var q in query)
{
Console.WriteLine(q == null ? "NULL VALUE" : q);
}
You want to skip where the element's value is null
string 您想跳过元素值为
null
字符串的地方
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.