【问题标题】:LINQ to XML Selecting attribute values that are not equal to a valueLINQ to XML 选择不等于某个值的属性值
【发布时间】:2019-07-24 20:14:06
【问题描述】:

为什么这不起作用?我正在尝试选择状态值不是“0”的属性。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
    <Auth status="0"></Auth>
    <Modify status="601"></Modify>
</response>

LINQ to XML

var errorcodeList = xml.Descendants("response")
                     .Where(x => x.Attribute("status").Value != "0")
                     .Select(x => x.Attribute("status").Value)
                     .ToList();

我原本希望得到“601”,但结果却没有得到任何元素。

【问题讨论】:

    标签: linq-to-xml


    【解决方案1】:

    这似乎是一个小问题:您正在尝试读取 response 元素的属性。

    以下代码访问response的后代并返回正确的值:

    var errorcodeList = xml.Descendants("response")
                           .Descendants()
                           .Where(x => x.Attribute("status").Value != "0")
                           .Select(x => x.Attribute("status").Value)
                           .ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 2016-12-19
      相关资源
      最近更新 更多