2010-07-22 59 views
1

的XPath时,我有以下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> 

将为获得所有角色名XPath表达式(ADM,USR)是什么样的?

这不是工作:

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

当我使用它,我得到的异常

'NS:响应/ NS:返回/斧:ROLENAME NS http://example.com斧头http://example.com/xsd' 有令牌无效。

回答

2

如果使用XmlDocument.SelectNodes方法,你应该使用"ns:response/ns:return/ax:roleName"作为XPath和命名空间添加到一个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); 
+0

我实际上使用XPathNodeIterator:XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator nodeIter = nav.Select(expression); – jwaliszko 2010-07-22 14:49:47

0

我想你只需要指定纳秒的incex:回报有不止一个:

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