StatsViaCsh
StatsViaCsh

Reputation: 2640

C#, escape double quote not working as expected

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

Answers (4)

Kyle Trauberman
Kyle Trauberman

Reputation: 25684

Add the @ prefix to your string.

@"//*[@id=""yfs_j10_a""]"

or escape the quotes with a \

"//*[@id=\"yfs_j10_a\"]"

Upvotes: 1

Trisped
Trisped

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

JaredPar
JaredPar

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

Peter Ritchie
Peter Ritchie

Reputation: 35881

Or use the escape char '\':

"//*[@id=\"yfs_j10_a\"]"

Upvotes: 2

Related Questions