首页 > 解决方案 > 为什么这个 XPathSelectElements() 返回 false?

问题描述

考虑下面的片段:

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#xmlxpathxelement

解决方案


问题出在你的元素上。

您也可以使用此 xml 对其进行测试

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

这将返回TRUE


推荐阅读