Reputation: 2640
I'm trying to escape quotes in an xpath string like so:
var mktCapNode = htmlDoc.DocumentNode.SelectSingleNode("//*[@id=""yfs_j10_a""]");
The actual string I want passed is:
//*[@id="yfs_j10_a"]
This gives a compiler errors: ) expected
and ; expected
I'm sure it's simple but I'm stumped. Any ideas?
Upvotes: 2
Views: 1417
Reputation: 25684
Add the @
prefix to your string.
@"//*[@id=""yfs_j10_a""]"
or escape the quotes with a \
"//*[@id=\"yfs_j10_a\"]"
Upvotes: 1
Reputation: 6003
In C# the \ character is used to escape (see documentation). This is different from VB where there are no escape characters except "" which escapes to ".
This means in C# you do not need vbCrLf to start a new line or vbTab to add a tab character to a string. Instead use "\r\n" and "\t".
You can also make the string a literal using the @ character, but I do not think this works with the quotation mark.
Upvotes: 1
Reputation: 754545
You need to make this a verbatim string to use the ""
as an escape
@"//*[@id=""yfs_j10_a""]"
For a normal string literal you need to use backslashes to escape the double quotes
"//*[@id=\"yfs_j10_a\"]"
Upvotes: 7