XPath 查找所有匹配项 C# XmlDocument
我试图弄清楚如何在 XmlDocument
中查找字符串的所有匹配项。
XmlNodeList results
= document.SelectNodes("Products/Product/fn:matches(.,'" + SearchWord + "')");
我正在尝试比较产品的innerText
。
虽然上面的例子不起作用,但我想我使用 XPath 函数的方式是非常错误的。
I am trying to figure out how to find all matches of string in a XmlDocument
.
XmlNodeList results
= document.SelectNodes("Products/Product/fn:matches(.,'" + SearchWord + "')");
Im trying to compare the innerText
of Product.
The above example don't work though, but I guess my way of using XPath functions are very wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
评估此 XPath 1.0 表达式(您知道
matches()
是一个 XPath 2.0 函数,.NET 不支持):这会选择所有具有文本的元素 -包含字符串
'YourSearchWord'
的节点子级,并且是Product
元素的后代,该元素是Products
元素的子级,而Products
元素是当前的孩子(上下文)节点。您可以使用以下内容组成 XPath 表达式:
但是,如果
SearchWord
是从用户输入中获取的,建议永远不要将其包含在上述骨架字符串中,以免 XPath 注入将被避免。如果是这种情况,建议的方法是使用预编译的 XPath 表达式,其中用户输入将作为变量引用,并且将从 XPath 计算上下文中使用该变量的值。
有关如何防止 XPath 注入的更多详细信息,请参阅此答案:
https://stackoverflow .com/a/6393690/36305
Evaluate this XPath 1.0 expression (did you know
matches()
is an XPath 2.0 function and isn't supported in .NET):This selects all elements that have a text-node-child that contains the string
'YourSearchWord'
and that are descendents of aProduct
element that is a child of aProducts
element that is a child of the current (context) node.You can compose the XPath expression with:
However, if
SearchWord
is obtained from user input, it is recommended never to include it in a skeletal string as above, so that XPath injection will be avoided.If this is the case, the recommended method is to have a precompiled XPath expression in which the user input will be referenced as a variable and the value of this variable will be consumed from the XPath evaluation context.
More details how to prevent an XPath injection can be found in this answer:
https://stackoverflow.com/a/6393690/36305
假设您有以下 xml
要获取所有节点,请使用 XPath 表达式 /Names/Name。第一个斜杠表示该节点必须是根节点。 SelectNodes 方法返回将包含节点的 XmlNodeList 集合。要获取子节点的值,您可以简单地使用节点名称索引 XmlNode:xmlNode["FirstName"].InnerText。
输出为:
姓名:John Smith
姓名:James White
以此作为示例/起点。谢谢
Lets say you have the following xml
To get all nodes use XPath expression /Names/Name. The first slash means that the node must be a root node. SelectNodes method returns collection XmlNodeList which will contain the nodes. To get value of sub node you can simply index XmlNode with the node name: xmlNode["FirstName"].InnerText.
The output is:
Name: John Smith
Name: James White
use this as an example / starting point. thanks
如果我正确理解您的问题,您可以使用以下内容:
"Products/Product/[.='" + SearchWord + "']"
或使用
contains()
等函数code>、starts-with()
或normalize-space()
(修剪并用一个空格替换连续的空格)"Products/Product/[contains (规范化空间(.), '" + 搜索词 + "')]"
If I understand your question correctly, you can use the following:
"Products/Product/[.='" + SearchWord + "']"
or use functions such as
contains()
,starts-with()
, ornormalize-space()
(trims and replaces consecutive white space with one space)"Products/Product/[contains(normalize-space(.), '" + SearchWord + "')]"
从答案
From the answer to this duplicate question: