/// <summary> /// This is used to get the string result from evaluating an XPath expression against the given document /// and a context created from a set of key/value pairs. /// </summary> /// <param name="document">The document to use</param> /// <param name="expression">The XPath expression to evaluate</param> /// <param name="keyValuePairs">A set of key/value pairs to use when creating the context</param> /// <returns>The evaluated expression result</returns> /// <example> /// <code language="cs"> /// string result = document.EvalXPathExpr("concat($key, '.htm')", "key", "filename"); /// </code> /// </example> /// <exception cref="ArgumentException">This is thrown if the <paramref name="keyValuePairs"/> /// parameter contains an odd number of parameters.</exception> public static string EvalXPathExpr(this IXPathNavigable document, XPathExpression expression, params string[] keyValuePairs) { if (keyValuePairs.Length % 2 != 0) { throw new ArgumentException("There must be a value for every key name specified", "keyValuePairs"); } CustomContext cc = new CustomContext(); for (int i = 0; i < keyValuePairs.Length; i += 2) { cc[keyValuePairs[i]] = keyValuePairs[i + 1]; } return(document.EvalXPathExpr(expression, cc)); }