我正在尝试使用 XPath 表达式在 XMLDocument
上执行 SelectSingleNode
。但是,当我搜索的属性值包含多个正斜杠 (/) 时,它返回 null。
我在网上找不到任何转义正斜杠的资源。有谁知道解决这个问题的方法吗?还是我的语法完全错误以匹配属性值?
示例 XML
<?xml version="1.0"?>
<Root>
<Page Path="/brand" />
<Page Path="/brand/armada" />
</Root>
此 XPath 表达式返回正确的节点(例如:上面示例中的第一个节点)
XmlNode N = xmlDoc.SelectSingleNode("Root/Page[@Path='/brand']");
这个 XPath 表达式返回 null
XmlNode N = xmlDoc.SelectSingleNode("Root/Page[@Path='/brand/armada']");
我在 C#、.net 3.5 环境中。
编辑:感谢您的回复。我通过在选择表达式中使用双正斜杠解决了这个问题。
XmlNode N = xmlDoc.SelectSingleNode("Root//Page[@Path='/brand/armada']");
最佳答案
感谢您的回复。我通过在选择表达式中使用双正斜杠解决了这个问题。
XmlNode N = xmlDoc.SelectSingleNode("Root//Page[@Path='/brand/armada']");
关于c# - 包含正斜杠 (/) 的属性的 XPath 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5895305/