C-Sharp function to escape single-quote on XPath expression
来源:互联网 发布:国内数据新闻栏目有 编辑:程序博客网 时间:2024/06/01 10:29
C#中,我们使用XPath 进行查询,比如:
string busname="Domino's Pizza";
xoEle = (XmlElement)xoDoc.DocumentElement.SelectSingleNode("//Review[RestaurantName='" + busname + "']");
由于有单引号,而会导致异常发生。
为了避免这种情况发生,应该使用concat函数来处理'''
//Review[RestaurantName=concat("Domino","'","s Pizza")]
对于单引号,替换为:" ' ";对于双银号,替换为:' " '
因此:
//不能正确处理单引号,双银号同时存在的
private static string ParseXpathString(string input)
{
string ret = "";
if (input.Contains("'"))
{
string[] inputstrs = input.Split('/'');
foreach (string inputstr in inputstrs)
{
if (ret != "")
ret += ",/"'/",";
ret += "/"" + inputstr + "/"";
}
ret = "concat(" + ret + ")";
}
else
{
ret = "'" + input + "'";
}
return ret;
}
xoEle = (XmlElement)xoDoc.DocumentElement.SelectSingleNode("//Review[RestaurantName="+ ParseXpathString(busname) + "]");
// It seems good code.
/// <summary>
/// Returns a valid XPath statement to use for searching attribute values regardless of 's or "s
/// </summary>
/// <param name="attributeValue">Attribute value to parse</param>
/// <returns>Parsed attribute value in concat() if needed</returns>
public static string GetXpathStringForAttributeValue(string attributeValue)
{
bool hasApos = attributeValue.Contains("'");
bool hasQuote = attributeValue.Contains("/"");
if (!hasApos)
{
return "'" + attributeValue + "'";
}
if (!hasQuote)
{
return "/"" + attributeValue + "/"";
}
StringBuilder result = new StringBuilder("concat(");
StringBuilder currentArgument = new StringBuilder();
for (int pos = 0; pos < attributeValue.Length; pos++)
{
switch (attributeValue[pos])
{
case '/'':
result.Append('/"');
result.Append(currentArgument.ToString());
result.Append("'/",");
currentArgument.Length = 0;
break;
case '/"':
result.Append('/'');
result.Append(currentArgument.ToString());
result.Append("/"/',");
currentArgument.Length = 0;
break;
default:
currentArgument.Append(attributeValue[pos]);
break;
}
}
if (currentArgument.Length == 0)
{
result[result.Length - 1] = ')';
}
else
{
result.Append("'");
result.Append(currentArgument.ToString());
result.Append("')");
}
return result.ToString();
}
- C-Sharp function to escape single-quote on XPath expression
- Escape both single quote and double quote in Smarty
- c sharp string to Enum
- oracle operator:Quote&Escape
- On quote
- Regular expression to match PHP function names
- c sharp
- C#(Sharp)
- getHeight() on Sharp V902
- VB Net To C Sharp Converter v2.16
- error C2276: '&' : illegal operation on bound member function expression
- error C2276: '&' : illegal operation on bound member function expression
- DB2 LIKE escape-expression
- xpath and regular expression
- C SHARP(C#)
- Play sounds on Sharp GX30
- c.sharp自定义事件
- effective c sharp 印象
- RTSP协议详解
- 身份证验证函数
- JSpider学习笔记(1)
- jstl fn标签
- 文本分类入门(八)中英文文本分类的异同
- C-Sharp function to escape single-quote on XPath expression
- 最近有点累
- 《编程之美》读书笔记集锦
- sizeof结构体和内存对齐
- Javascript - Select操作大集合
- winform部署发布应用程序
- CComVariant
- 可变函数名调用方式
- Oracle的RBO/CBO优化器