2010-07-22 64 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