我试图从VB.Net中的Alfresco WSDL读取SOAP地址。
XML看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns:cmis="http://docs.oasis-open.org/ns/cmis core/200908/" xmlns:cmism="http://docs.oasis-open.org/ns/cmis/messaging/200908/" xmlns:cmisw="http://docs.oasis-open.org/ns/cmis/ws/200908/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:ns="http://schemas.xmlsoap.org/soap/encoding/" targetNamespace="http://docs.oasis-open.org/ns/cmis/ws/200908/" xmlns:jaxws="http://java.sun.com/xml/ns/jaxws" name="CMISWebServices">
...
<message name="cmisException">
<part name="fault" element="cmism:cmisFault" />
</message>
...
<service name="ACLService">
<port name="ACLServicePort" binding="cmisw:ACLServicePortBinding">
<soap:address location="https://myserver:8443/alfresco/cmisws/ACLService" />
</port>
</service>
...
这是我的VB.Net代码。我试图将XmlDocument.SelectNodes()
与XPath查询一起使用......而且我没有快乐。
Dim xmlReader As System.Xml.XmlTextReader = New XmlTextReader(urlWsdl)
Dim xmlWsdl As System.Xml.XmlDocument = New XmlDocument
xmlWsdl.Load(xmlReader)
...
Dim xPath As String = "/definitions/service[@name='" & sService & "']"
Dim serviceNodes As XmlNodeList
While True
Try
serviceNodes = xmlWsdl.SelectNodes(xPath)
PrintMsg("Count=" & serviceNodes.Count)
Catch ex As Exception
Logger.LogMsg("ERROR: " & ex.Message)
End Try
End While
...
我需要从WSDL获取几个不同服务的地址位置:&#34; ACLService&#34;,&#34; DiscoveryService&#34;,&#34; MultiFilingService&#34;,&#34; NavigationService& #34;等等。
我尝试了很多不同的XPath表达式,但我总是得到&#34; serviceNodes.Count&#34; &#34; 0&#34;:
Dim serviceNodes As XmlNodeList = xmlWsdl.SelectNodes(xPath):
XPath: Count: ServicesNodes(0).OuterXml:
----- ----- -------------------------
//service[@name='ACLService'] 0
//service[@name=ACLService] 0
//service 0
* 1 OuterXml: "<definitions ...>... // Entire XML document
/definitions//service[@name=ACLService] 0
/definitions/service[@name='ACLService'] 0
/definitions/service 0
问:我做错了什么?
问:我能做些什么来解决它?
=============================================== ====
非常感谢har07,他正确地指出我需要使用XmlNamespaceManager。
以下是更新后的代码:
Dim nsManager As New XmlNamespaceManager(New NameTable())
nsManager.AddNamespace("afws", xmlWsdl.DocumentElement.NamespaceURI)
Dim xPath As String = "/afws:definitions/afws:service[@name='" & sService & "']"
Dim serviceNodes As XmlNodeList = xmlWsdl.SelectNodes(xPath, nsManager) ' Now works perfectly!
答案 0 :(得分:3)
&#34;我做错了什么?&#34;
您的XPath没有考虑默认命名空间,在此处声明没有前缀的那个:xmlns="http://schemas.xmlsoap.org/wsdl/"
。 (有关详细信息,请参阅this MSDN article中的 默认命名空间 部分)。
&#34;我该怎么做才能修复它?&#34;
使用XmlNamespaceManager
注册前缀到默认命名空间URI的映射,然后在XPath中正确使用已注册的前缀,例如:
....
Dim nsManager As New XmlNamespaceManager(New NameTable())
nsManager.AddNamespace("d", xmlWsdl.DocumentElement.NamespaceURI)
Dim xPath As String = "/d:definitions/d:service[@name='" & sService & "']"
....
serviceNodes = xmlWsdl.SelectNodes(xPath, nsManager)