Solved

C#: how to use xpath to escape a token

Posted on 2014-04-30
5
812 Views
Last Modified: 2014-05-06
how do i use the below:

XmlNode node = doc.SelectSingleNode("/edt/ce[normalize-space(desc)='" + msg + "']/code");

in the event that the msg parameter contains an apostrophe or other special characters (since it is a string). (i.e. - Can't create Server object). This string exists in the xml . the <desc> tag does contain  special characters as well (since it's a string).

how do i escape special grammar characters like commas ,colon, parenthesis,  dash, slash , underscore)
0
Comment
Question by:badtz7229
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 18

Expert Comment

by:zc2
ID: 40032721
Replace the invalid characters in the msg with entities, something like
msg = msg.replace( "'", "&#x27;" )
0
 

Author Comment

by:badtz7229
ID: 40032729
if i use a replace it won't find it in the xml then.
for example
msg =  Can't create Server object

and my xml file contains

<edt name="errors">
      <ce>
            <code>111</code>
            <desc>Not here</desc>
      </ce>
      <ce>
            <code>222</code>
            <desc>Can't create Server object </desc>
      </ce>
</edt>
0
 
LVL 18

Accepted Solution

by:
zc2 earned 500 total points
ID: 40032786
You are right, entity does not work.
If the apostrophe only could be in the string, then we could avoid using it in the xpath:
XmlNode node = doc.SelectSingleNode("/edt/ce[normalize-space(desc)=\"" + msg + "\"]/code");
0
 

Author Comment

by:badtz7229
ID: 40032801
@ zc2
your last comment worked successfully.
thanks.
0
 
LVL 18

Expert Comment

by:zc2
ID: 40032848
This should work even if the search string contains both single and double quote characters:
            msg = "concat('" + msg.Replace( "'", "',\"'\",'" ) + "')";
            XmlNode node = doc.SelectSingleNode("/edt/ce[normalize-space(desc)=" + msg + "]/code");

Open in new window

0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question