繁体   English   中英

LINQ查询以在不存在属性时获取XML元素

[英]LINQ query to get XML elements when an attribute is not present

我有这个XML:

<Config>
  <EmpFieldsMap>
    <Employee>
      <Field>
        <Name update = "false">EmpNumber</Name>
      </Field>
      <Field>
        <Name insert = "true">EmpName</Name>
      </Field>
      <Field>
        <Name insert = "true">EmpDesignation</Name>
      </Field>
    </Employee>
  </EmpFieldsMap>
</Config>

我的应用程序将执行INSERT或UPDATE,其字段将来自此xml。 每个标签都将具有insert或update属性,如上面的代码片段所示。

对于插入具有属性的所有标签

insert = "true"

并且必须考虑不具有此属性的标签(在这种情况下为“ EmpNumber”)。

更新同样如此。

这段代码为我提供了insert属性设置为true的所有标签:

insertTags = from p in xml.Element("Config").Element("EmpFieldsMap").Elements("Field")
             where p.Element("Name").Attribute("insert") != null 
             && p.Element("Name").Attribute("insert").Value == "true"
             select p.Element("Name").Value;

删除空值检查

insertTags = from p in xml.Element("Config").Element("EmpFieldsMap").Elements("Field")
             where p.Element("Name").Attribute("insert").Value == "true"
             select p.Element("Name").Value;

对象引用未设置为实例

错误。

我在编写查询时遇到麻烦,该查询还将包含不存在该属性的标签。

有人可以帮我吗?

问候。

insertTags = from p in xml.Element("Config").Element("EmpFieldsMap").Elements("Field")
    where (p.Element("Name").Attribute("insert") ?? "true") == "true"
    select p.Element("Name").Value;

使用XPath和Linq,它甚至更简单:

XPathSelectElements(@"Config/EmpFieldsMap/Employee/Field/Name[@insert='true']")

同样对于这个特定的xml,您可以使用全局搜索来查找名称元素:

var insertTags = xdoc.XPathSelectElements(@"//Name[@insert='true']")
                     .Select(n => (string)n);

或使用Linq查询语法:

var insertTags = from n in xdoc.Descendants("Name")
                 where (string)n.Attribute("insert") == "true"
                 select (string)n;

将节点值转换为字符串时,如果缺少节点,则不会引发异常。 简单地将返回null 因此,您不需要所有这些东西(顺便说一句,甚至是不可编译的 ):

(p.Element("Name").Attribute("insert") ?? "true") == "true"

再编辑一遍 如果要处理布尔值,请使用布尔值而不是字符串:

var insertTags = from n in xdoc.Descendants("Name")
                 where (bool?)n.Attribute("insert") == true
                 select (string)n;

这个怎么运作? 可为空的布尔值将对缺少的属性具有null值。 比较bool? 没有任何布尔值的值将生成false 因此,您将仅获得具有必需属性且对该属性具有true那些元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM