1

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.

0

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I'm actually using XPathNodeIterator: XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator nodeIter = nav.Select(expression);
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.