【问题标题】:Why this XPathSelectElements() returns false?为什么这个 XPathSelectElements() 返回 false?
【发布时间】:2018-07-05 20:31:56
【问题描述】:

考虑下面的 sn-p:

var xpath = "//i[@a='1']";
var item = new XElement("i",
    new XAttribute("a", "1"),
    new XAttribute("b", "2"),
    new XAttribute("c", "3"));

Console.WriteLine(item); // <i a="1" b="2" c="3" />
Console.WriteLine("{0} = {1}", xpath, item.XPathSelectElements(xpath).Any());

我原以为.Any() 结果是true,但我不断收到false

【问题讨论】:

    标签: c# xml xpath xelement


    【解决方案1】:

    使用self::i[@a='1'] 或将XElement 添加到XDocument 中就可以了。

    【讨论】:

      【解决方案2】:

      问题在于您的 root 元素。

      你也可以用这个xml测试一下

      var item = new XDocument(new XElement("i",
                      new XAttribute("a", "1"),
                      new XAttribute("b", "2"),
                      new XAttribute("c", "3")));
      

      这将返回 TRUE

      【讨论】:

      • i[@a='1'] = False
      • 尝试匹配 根项 应该对 // 轴没有影响。
      • 试试 sn-p ;)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      • 2012-07-09
      • 2010-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多