jwaliszko
jwaliszko

Reputation: 17064

Invalid token error when using XPath with namespaces

I have the following XML:

<ns:response xmlns:ns="http://example.com" xmlns:ax="http://example.com/xsd" >
    <ns:return type="mytype">
        <ax:roleID>1</ax:roleID>
        <ax:roleName>ADM</ax:roleName>
    </ns:return>
    <ns:return type="mytype">
        <ax:roleID>2</ax:roleID>
        <ax:roleName>USR</ax:roleName>
    </ns:return>
</ns:response>

What would an XPath expression for getting all roleNames (ADM, USR) look like?

This is not working:

ns:response/ns:return/ax:roleName ns http://example.com ax http://example.com/xsd

When I use it, I get the exception

'ns:response/ns:return/ax:roleName ns http://example.com ax http://example.com/xsd' has an invalid token.

Upvotes: 1

Views: 1123

Answers (2)

Hinek
Hinek

Reputation: 9709

If you are using XmlDocument.SelectNodes method, you should use "ns:response/ns:return/ax:roleName" as XPath and add the namespaces to an XmlNamespaceManager:

man.AddNamespace("ns", "http://example.com");
man.AddNamespace("ax", "http://example.com/xsd");
var set = doc.SelectNodes("ns:response/ns:return/ax:roleName", man);

Upvotes: 2

Paul Michaels
Paul Michaels

Reputation: 16685

I think you just need to specify the incex of ns:return as there's more than one:

ns:response/ns:return[1]/ax:roleName

Upvotes: 0

Related Questions