Invalid Token when using XPath
-
Hi I am making a modification to a web application using XPath, and when executed I get an error message - Invalid token! This is basic what I am doing public xmlNode GetSelection (SelectParams params, xmldocument docment) { xpathstring = string.format("Name =\'{0}' Displaytag = \'{1}' Manadatory=\'{2}', params.Name, params.Displaytag, params.Manadatory); return document.selectsinglenode(xpathstring); } As you can see, I am making a string and setting values on the nodes I am trying to find against my xml document, and thus returning xml data that matches my parameters. What is happening is that I am getting an xpathexeception error in Visual Studio and it says invalid token. I do know that in the xml document that the parameters I am looking in the tags have double quotes, for example, Name="ABC". So, I thought the problem could be solved using an "\". Can anyone help?
-
Hi I am making a modification to a web application using XPath, and when executed I get an error message - Invalid token! This is basic what I am doing public xmlNode GetSelection (SelectParams params, xmldocument docment) { xpathstring = string.format("Name =\'{0}' Displaytag = \'{1}' Manadatory=\'{2}', params.Name, params.Displaytag, params.Manadatory); return document.selectsinglenode(xpathstring); } As you can see, I am making a string and setting values on the nodes I am trying to find against my xml document, and thus returning xml data that matches my parameters. What is happening is that I am getting an xpathexeception error in Visual Studio and it says invalid token. I do know that in the xml document that the parameters I am looking in the tags have double quotes, for example, Name="ABC". So, I thought the problem could be solved using an "\". Can anyone help?
Hi, As far as I can see, you're trying to escape a single quote, not a double quote. Wouldn't be much more acute to use double quote for the construction of your XPath string ?
xpathstring = string.format("Name=\"{0}\" Displaytag=\"{1}\" Mandatory=\"{2}\"", params.Name, params.Displaytag, params.Mandatory);
Remark 1 : Was your code snippet weel-formed ? Because the way you showed it to us, compiler wouldn't even accept to compile (you never close the string with the closing double-quote). Remark 2 : If you are escaping the first single-quote, why don't you escape the second also ? Remark 3 : While debugging, could you test for the value of xpathstring ? What does it show ? Remark 4 : When you publish some code, please take the time to format it well and use <pre> tag.