MJ Nine
MJ Nine

Reputation: 11

How to escape XPath quotes "" with C#

I'm trying to scrape Pinterest using XPath and HTMLAgilityPack, but XPath writes "" (//*(@id="XX") to represent the id attribute value, so in C# the strings are split and I get a syntax error. ("["@id="XX(error)"]") What should I do?

Upvotes: 1

Views: 502

Answers (1)

kjhughes
kjhughes

Reputation: 111541

First, fix your XPath syntax error: Change //*(@id="XX" to //*[@id="XX"].

Then, to represent //*[@id="XX"] as a C# string, use either C# escaping or XPath alternative attribute value delimiters.

C# escaping

Escape the " in C# via \"

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

or via verbatim string literal syntax

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

XPath alternative attribute value delimiters

Use ' as the attribute value delimiter, which both XML and XPath allow:

"//*[@id='XX']"

See also

Upvotes: 1

Related Questions