LINQ-to-XML XElement查询为NULL [英] LINQ-to-XML XElement query NULL

查看:98
本文介绍了LINQ-to-XML XElement查询为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆