Encoding XPath Expressions with both single and double quotes

前端 未结 8 1194
孤街浪徒
孤街浪徒 2020-11-30 07:21

XPath (v1) contains no way to encode expressions.

If you only have single OR double quotes then you can use expressions such as

//review[@name=\"Bob         


        
8条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 07:53

    Wow, you all sure are making this complicated. Why not just do this?

    public static string XpathExpression(string value)
    {
        if (!value.Contains("'"))
            return '\'' + value + '\'';
    
        else if (!value.Contains("\""))
            return '"' + value + '"';
    
        else
            return "concat('" + value.Replace("'", "',\"'\",'") + "')";
    }
    

    .NET Fiddle & test

提交回复
热议问题