help for xpath with '\'

P

Praveen

I have an xml with attribute value contains '\'.
<Root>
<I Attr= "A.B\TEST1INDX\C.L\TEST2INDX\M.N."/>

how to do xpath for this attribute
A="A.B\TEST1INDX\C.L\TEST2INDX\M.N."
selectNodes("I[@Attr=' "+A+" ']")
but this fails...
how to escape this

thanks
Praveen
 
M

Martin Honnen

Praveen said:
I have an xml with attribute value contains '\'.
<Root>
<I Attr= "A.B\TEST1INDX\C.L\TEST2INDX\M.N."/>

how to do xpath for this attribute
A="A.B\TEST1INDX\C.L\TEST2INDX\M.N."
selectNodes("I[@Attr=' "+A+" ']")
but this fails...
how to escape this

With C# you can prefix the string literal with @ e.g.
A = @"A.B\TEST1INDX\C.L\TEST2INDX\M.N.";
you you need to use \\ to escape the backslash e.g.
A = "A.B\\TEST1INDX\\C.L\\TEST2INDX\\M.N.";
 
S

Steven Cheng[MSFT]

Thanks for Martin's informative input.

Hi Praveen,

As Martin has said, the xpath with '\' char problem here is due to the
escape requirement of C# string rather than XML/XPATH. In other words, you
do not need to escape '\' char in XML/XPATH document, but need to escape it
in C# string. In c# string, you need to either use the "@" prefix such as

string path = @"c:\temp\data\";

or use "\\" to escape "\" without using "@" prefix, e.g.

string path = "c:\\temp\\data\\";


http://www.softsteel.co.uk/tutorials/cSharp/lesson4.html


In addition, for XML document here has introduced those characters that
need escaping in XML document:

#Technical Note: Escape Characters for XML/HDF5
http://hdf.ncsa.uiuc.edu/HDF5/XML/xml_escape_chars.htm

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top