c# - 为什么这个 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
。
解决方案
问题出在你的根元素上。
您也可以使用此 xml 对其进行测试
var item = new XDocument(new XElement("i",
new XAttribute("a", "1"),
new XAttribute("b", "2"),
new XAttribute("c", "3")));
这将返回TRUE
推荐阅读
- bash - Nested function in Bash: why does return stop parent function?
- mysql - dyld:库未加载:/usr/local/opt/unixodbc/lib/libodbc.2.dylib
- react-native - 我第一次尝试运行 React Native 应用程序,但我遇到了一个错误。undefined 不是函数(评估“Object.getOwnPropertyDescriptors”)
- jquery - Changing The Arrow Icon In Dropdown To any Icon I want
- python - OpenCV VideoCapture.retrieve() limits fps to 30 with DSHOW & MJPEG (adds delay) -- how to get around it?
- mysql - 请问如何找到练习被sql打断的人?
- python - Adding returning form_invalid (or something) with custom form errors in custom signup form with django-allauth
- excel - 是否有 Excel VBA 函数来调整文本大小以适应形状?
- python - 在 sqlalchemy 查询中使用 jsonb_array_elements
- multithreading - 如何使场景在批处理脚本中发生