teenup
teenup

Reputation: 7667

Compiling an xpath expression - has an invalid token

I am trying to compile an xpath expression that is made dynamically and receiving an exception that - "it has an invalid token".

text =  GetText(); // has this value: /root/Info/PublisherName1 = "abcd"
XPathResultType result = XPathExpression.Compile(text).ReturnType;

I have tried escaping the double quotes using the following code, but still receiving the same exception:

text = text.Replace("\"", "\\\"");
text = text.Replace("'", "\\'");

After trying to escape the quotes as above, the string still have the single quotes inside without showing any backslash when seen in the debugger.

How can this be done?

Upvotes: 1

Views: 1168

Answers (2)

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

/root/Info/PublisherName1 = "abcd" is invalid XPath for methods from System.Xml.XPath namespace

I believe you are looking for something like this: /root/Info/PublisherName1[text() = "abcd"]

Upvotes: 3

teenup
teenup

Reputation: 7667

The Expression is a valid XPath expression, the problem is the quotes are coming from a Microsoft Word file and we need to replace smart quotes with straight quotes as follows:

text = text.Replace('\u2018', '\'')
.Replace('\u2019', '\'')
.Replace('\u201c', '\"')
.Replace('\u201d', '\"')
.Replace('\u201e', '\"')
.Replace('\u2033', '\"');

Then, it works without any problem.

Sorry @Kirill, I have to deselect your answer.

Upvotes: 1

Related Questions