Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an alternative to Robert Rossney's StringBuilder approach, perhaps more intuitive:</p> <pre><code> /// &lt;summary&gt; /// Produce an XPath literal equal to the value if possible; if not, produce /// an XPath expression that will match the value. /// /// Note that this function will produce very long XPath expressions if a value /// contains a long run of double quotes. /// /// From: http://stackoverflow.com/questions/1341847/special-character-in-xpath-query /// &lt;/summary&gt; /// &lt;param name="value"&gt;The value to match.&lt;/param&gt; /// &lt;returns&gt;If the value contains only single or double quotes, an XPath /// literal equal to the value. If it contains both, an XPath expression, /// using concat(), that evaluates to the value.&lt;/returns&gt; public static string XPathLiteral(string value) { // If the value contains only single or double quotes, construct // an XPath literal if (!value.Contains("\"")) return "\"" + value + "\""; if (!value.Contains("'")) return "'" + value + "'"; // If the value contains both single and double quotes, construct an // expression that concatenates all non-double-quote substrings with // the quotes, e.g.: // // concat("foo",'"',"bar") List&lt;string&gt; parts = new List&lt;string&gt;(); // First, put a '"' after each component in the string. foreach (var str in value.Split('"')) { if (!string.IsNullOrEmpty(str)) parts.Add('"' + str + '"'); // (edited -- thanks Daniel :-) parts.Add("'\"'"); } // Then remove the extra '"' after the last component. parts.RemoveAt(parts.Count - 1); // Finally, put it together into a concat() function call. return "concat(" + string.Join(",", parts) + ")"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload