6

I'm trying to do a SelectSingleNode on an XMLDocument using an XPath expression. However, when the attribute value that I'm searching contains multiple forward slashes (/), it returns null.

I can't find any resources online for escaping the forward slash. Does anyone know a way around this? Or have I got my syntax all wrong for matching the attribute value?

Example XML

<?xml version="1.0"?>
  <Root>
    <Page Path="/brand" />
    <Page Path="/brand/armada" />
  </Root>

This XPath expression returns the correct node (e.g.: the first one in the above sample)

XmlNode N = xmlDoc.SelectSingleNode("Root/Page[@Path='/brand']");

This XPath expression returns null

XmlNode N = xmlDoc.SelectSingleNode("Root/Page[@Path='/brand/armada']");

I'm in a C#, .net 3.5 environment.

EDIT: Thanks for the responses. I solved the issue by using a double forward slash in the select expression.

XmlNode N = xmlDoc.SelectSingleNode("Root//Page[@Path='/brand/armada']");
Burkart
  • 450
  • 4
  • 9
Chris
  • 121
  • 1
  • 8
  • My minimal repro console app gives the expected results from both xpaths – AakashM May 05 '11 at 09:48
  • Must be a bug. Or the problem is anywhere else. XMLspy 2011 sp1 does it correct with your Xpath expressions. – ceving May 05 '11 at 14:18
  • 4
    @chris if you have found the answer yourself, can you post the answer below, and mark it as the accepted answer, so other can benefit. This will also mean that this question no longer appears in the "Unanswered" section of SO. Thanks – BMac May 11 '15 at 07:49
  • Possible duplicate of [How do I escape an apostrophe in my XPath text query with Perl and Selenium?](http://stackoverflow.com/questions/3124785/how-do-i-escape-an-apostrophe-in-my-xpath-text-query-with-perl-and-selenium) – BMac Feb 15 '16 at 08:43
  • It would be great to post the answer and mark it. Just for the sake of cleaning the list. – Honza Hejzl Mar 22 '16 at 10:08

1 Answers1

1

Thanks for the responses. I solved the issue by using a double forward slash in the select expression.

XmlNode N = xmlDoc.SelectSingleNode("Root//Page[@Path='/brand/armada']");
Chris
  • 121
  • 1
  • 8