473,473 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

invalid token XPath expression

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

Any suggestions?
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <root>
  3. <Japan>ユーザーおよび管理者</Japan>
  4. <English>'Guest' and "Administrator"</English>
  5. </root>
  6. <root>
  7. <Japan>........</Japan>
  8. <English>.............</English>
  9. </root>
Here is my code:
Expand|Select|Wrap|Line Numbers
  1. // Create an XPathExpression
  2. string strEnglish = "'Guest' and \"Administrator\"";
  3. xPathExpr = nav.Compile( string.Format( "//root[English={0}\"]/Japan", strEnglish) ); 
May 21 '10 #1
2 6244
nukefusion
221 Recognized Expert New Member
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:

Expand|Select|Wrap|Line Numbers
  1. string strEnglish = "concat(\"'Guest' and \", '\"Administrator\"')";
  2.  
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.
May 21 '10 #2
Whileoflife
2 New Member
Thanks nukefusion,
I have function to generate concat for XPath:
Expand|Select|Wrap|Line Numbers
  1. private string GenerateConcatForXPath( string a_xPathQueryString )
  2.         {
  3.             string returnString = string.Empty;
  4.             string searchString = a_xPathQueryString;
  5.             char[] quoteChars = new char[] { '\'', '"' };
  6.             int quotePos = searchString.IndexOfAny( quoteChars );
  7.             if ( quotePos == -1 )
  8.             {
  9.                 returnString = "'" + searchString + "'";
  10.             }
  11.             else
  12.             {
  13.                 returnString = "concat(";
  14.                 while ( quotePos != -1 )
  15.                 {
  16.                     string subString = searchString.Substring( 0, quotePos );
  17.                     returnString += "'" + subString + "', ";
  18.                     if ( searchString.Substring( quotePos, 1 ) == "'" )
  19.                     {
  20.                         returnString += "\"'\", ";
  21.                     }
  22.                     else
  23.                     {
  24.                         //must be a double quote
  25.                         returnString += "'\"', ";
  26.                     }
  27.                     searchString = searchString.Substring( quotePos + 1,
  28.                                      searchString.Length - quotePos - 1 );
  29.                     quotePos = searchString.IndexOfAny( quoteChars );
  30.                 }
  31.                 returnString += "'" + searchString + "')";
  32.             }
  33.             return returnString;
  34.         }
To use:
Expand|Select|Wrap|Line Numbers
  1. // Create an XPathExpression 
  2. string strEnglish = GenerateConcatForXPath("'Guest' and \"Administrator\""); 
  3. xPathExpr = nav.Compile( string.Format( "//root[English={0}\"]/Japan", strEnglish) );
May 21 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Anna | last post by:
Hi all. I hav eproblems constructing one XPath expression. Here is what I want to do: I want to choose all area tags that belong to a certain image map, having src of the image map. I don't want...
2
by: GIMME | last post by:
For starters .... If : //input is the xpath syntax to see if an element with an attribute named start_date exists ... Then what is the xpath syntax to return elements having
1
by: kurt hansen | last post by:
hi I thought that this would be easy, but maybe not so much. I want to: pass an xpath expression and a string value to a stylesheet and copy the source xml document, changing the value of...
3
by: gimme_this_gimme_that | last post by:
I once downloaded a shareware program that allowed you to open an xml file, click on a text or an attribute, an then see the xpath expression that would fetch that data. The program didn't...
2
by: gimme_this_gimme_that | last post by:
What xpath expression would return the category-item having uid sps002 ? <category-list> <category> <uid>GRIDS_MAIN_CATEGORY_UID</uid> <uid-type>Categories</uid-type> <category-item-list>...
7
by: steve bull | last post by:
I have the following code snippet to read the colorRange attributes for the colorRangeSwatch in the xml file listed below. string expr = "/swatches/colorRangeSwatch/colorRange";...
1
by: rustyfancy | last post by:
I have a textBox that contains a string values. I need to take this string and use it as criteria for an XPath Expression. Here's what I have. Why isn't it working??? string s =...
1
by: Steve | last post by:
Hello, I get an error in my xslt document using vs.net 2005 and Xml Spy. Any help is greatly appreciated! ================ Error: ================ {"'$Field/name()' is an invalid XPath...
2
by: Paul | last post by:
I'd like to write an XPath expression that gets all nodes with a given local-name, say 'Customer', within a given namespace. From a prior response I got the following to get nodes with a specific...
1
by: miamikk | last post by:
I have created XML files using BCP in SQL with FOR XML AUTO, ELEMENTS option. When I try to databind for XML file to a GridView in ASP.NET, I get an error " 'd/Commodity Description' has an...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.