LINQ-to-XML XElement查询为NULL [英] LINQ-to-XML XElement query NULL
问题描述
我正在尝试UPDATE
某事物的子元素(在本例中为"Regex").WHERE
其中一个子元素(名称")==选择的名称("AccountNumber").
I am trying to UPDATE
a child element of something (in this case, "Regex") WHERE
one of the child elements ("Name") == selected name ("AccountNumber").
这是我的XmlDoc的示例
Here is a sample of my XmlDoc
<?xml version="1.0" encoding="utf-8"?>
<Bill>
<Element>
<Name>AccountNumber</Name>
<Regex></Regex>
<Left></Left>
<Right></Right>
<Top></Top>
<Bottom></Bottom>
<Relations></Relations>
</Element>
<Element>
<Name>BillDate</Name>
<Regex></Regex>
<Left></Left>
<Right></Right>
<Top></Top>
<Bottom></Bottom>
<Relations></Relations>
</Element>
</Bill>
这是我到目前为止的代码.
and here is the code I have so far.
XElement x = XmlDoc.Element("Bill")
.Elements("Element")
.Where(xel => xel.Element("Name").ToString() == CurrentSelection.ElementName)
.SingleOrDefault();
x.Element("Regex").Value = details[1].Value;
查询运行后,XElement x仍然为空...我对LINQ(和Lambdas)很陌生,可以在这里使用一些指导. 谢谢!
After the query runs, the XElement, x, is still null... I am very new to LINQ (and Lambdas) and could use a little guidance here. Thanks!
推荐答案
它会返回null,因为您将Element转换为String,而不是它的值.您应该像这样检查子Element值
It returns null because you Convert Element to String,not it's value.You should check child Element value like this
xel.Element("Name").Value.ToString() == CurrentSelection.ElementName
我认为Value返回字符串,所以ToString
在这里只是键入
And i think Value returns string so ToString
is redundant here just type
xel.Element("Name").Value == CurrentSelection.ElementName
这篇关于LINQ-to-XML XElement查询为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!