开发者

Invalid token error when using XPath with namespaces

开发者 https://www.devze.com 2023-01-08 04:53 出处:网络
I have the following XML: <ns:response xmlns:ns=\"http://example.com\" xmlns:ax=\"http://example.com/xsd\" >

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:r开发者_开发百科esponse>

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.


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);


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
0

精彩评论

暂无评论...
验证码 换一张
取 消