Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath query for an attribute containing forward slash (/)

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']");
like image 676
Chris Avatar asked May 05 '11 09:05

Chris


Video Answer


1 Answers

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']");
like image 103
Chris Avatar answered Oct 15 '22 01:10

Chris