invalid token XPath expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Whileoflife
    New Member
    • May 2010
    • 2

    invalid token XPath expression

    When I run the code below I get the error: has an invalid token

    Any suggestions?
    Code:
    <?xml version="1.0"?>
    <root>
    <Japan>ユーザーおよび管理者</Japan>
    <English>'Guest' and "Administrator"</English>
    </root>
    <root>
    <Japan>........</Japan>
    <English>.............</English>
    </root>
    Here is my code:
    Code:
    // Create an XPathExpression
    string strEnglish = "'Guest' and \"Administrator\"";
    xPathExpr = nav.Compile( string.Format( "//root[English={0}\"]/Japan", strEnglish) );
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    Hi and welcome to the forums,

    First of all, your XML document is technically invalid as it doesn't have a single root element. With the samples you've posted your current code will more than likely fail with a runtime exception stating that. So first you need to add a root element that encompasses all others.

    The problem with your XPath here is that you have both single and double quotes in the element value. There's a trick you can use to get around this using the XPath concat function, you break up the string into sections using only one type of quote and join them like so:

    Code:
    string strEnglish = "concat(\"'Guest' and \", '\"Administrator\"')";
    Providing you add a proper root element to your XML document, this should locate the node you're looking for.

    In the future however, if you have any control over the XML document you might want to consider looking into the escape codes &apos; and &quot; instead. Could save you some hassle.

    Hope this helps.

    Comment

    • Whileoflife
      New Member
      • May 2010
      • 2

      #3
      Thanks nukefusion,
      I have function to generate concat for XPath:
      Code:
      private string GenerateConcatForXPath( string a_xPathQueryString )
      		{
      			string returnString = string.Empty;
      			string searchString = a_xPathQueryString;
      			char[] quoteChars = new char[] { '\'', '"' };
      			int quotePos = searchString.IndexOfAny( quoteChars );
      			if ( quotePos == -1 )
      			{
      				returnString = "'" + searchString + "'";
      			}
      			else
      			{
      				returnString = "concat(";
      				while ( quotePos != -1 )
      				{
      					string subString = searchString.Substring( 0, quotePos );
      					returnString += "'" + subString + "', ";
      					if ( searchString.Substring( quotePos, 1 ) == "'" )
      					{
      						returnString += "\"'\", ";
      					}
      					else
      					{
      						//must be a double quote
      						returnString += "'\"', ";
      					}
      					searchString = searchString.Substring( quotePos + 1,
      									 searchString.Length - quotePos - 1 );
      					quotePos = searchString.IndexOfAny( quoteChars );
      				}
      				returnString += "'" + searchString + "')";
      			}
      			return returnString;
      		}
      To use:
      Code:
      // Create an XPathExpression 
      string strEnglish = GenerateConcatForXPath("'Guest' and \"Administrator\""); 
      xPathExpr = nav.Compile( string.Format( "//root[English={0}\"]/Japan", strEnglish) );
      Last edited by Whileoflife; May 22 '10, 02:36 AM. Reason: Update code

      Comment

      Working...