gpt4 book ai didi

c# - 用于查找具有非法字符的属性的 XPath

转载 作者:太空宇宙 更新时间:2023-11-03 13:23:18 25 4
gpt4 key购买 nike

我正在尝试使用 XPath 查找具有特定值属性的节点。下面的代码适用于普通字符串,但在存在转义字符(如 & 或 ')时会失败。

string query = "question & answer";
query = query.replace("&", "&");

string nodePath = string.Format("/QUESTIONS/QUESTION[@text='{0}']", query);

XmlNode node = xmlDoc.SelectSingleNode(nodePath);

有没有办法找到值包含转义字符的属性?

最佳答案

只是不要替换特殊字符:

string query = "question & answer";
string nodePath = string.Format("/QUESTIONS/QUESTION[@text='{0}']", query);
XmlNode node = xmlDoc.SelectSingleNode(nodePath);

这将匹配

<QUESTION text="question &amp; answer"/>

如果您需要搜索带有单引号字符的内容,则对属性文本使用双引号:

string query = "question's answer";
string nodePath = string.Format("/QUESTIONS/QUESTION[@text=\"{0}\"]", query);

匹配

<QUESTION text='question&apos;s answer'/>

如果文本中有单引号,只需对文本属性值使用双引号。

关于c# - 用于查找具有非法字符的属性的 XPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23361094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com