tags:

views:

77

answers:

3

Suppose I have this XML document...

<root>
  <str name="My node's attribute">My string's value</str>
</root>

I want to get the value of the "str" element based on a known value of the elements "name" attribute so I use the following XPath query...

str[@name='My node's attribute']

But that doesn't work for me at least not in classic ASP and C# because of the single quote which of course conflicts with the single quotes used in XPath syntax. An exception is thrown in both cases.

What is a possible solution here if I have the restriction that I can't change the XML document.

A: 

try

str[@name='My node''s attribute']
Ian Jacobs
Doubling up the ' character is not an escape sequence understood by XPath.
AnthonyWJones
+2  A: 

Use " in the xpath instead:-

C#

 string xpath = "str[@name=\"My node's attribute\"]";

VBScript

 Dim xpath : xpath = "str[@name=""My node's attribute""]"
AnthonyWJones
Or C# `string xpath = @"str[@name=""My node's attribute""]";`
Tomalak
The VBScript version worked for me. Thanks alot! C# is returning nothing for me though. I would imagine C# works for you fine Anthony?
Rob Segal
A: 
str[@name='My node&apos;s attribute']
Aito
In raw XPath as executed against an XmlDocument the XML entities are not understood. In XSL it will be unescaped before the XPath is parsed resulting in the same problem.
AnthonyWJones