VB.Net XmlDocument.SelectNodes(xPath) : unable to query WSDL

标签 vb.net xpath xmldocument

我正在尝试从 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 获取几个不同服务的地址位置:“ACLService”、“DiscoveryService”、“MultiFilingService”、“NavigationService”等。

我尝试了许多不同的 XPath 表达式,但总是得到“0”的“serviceNodes.Count”:
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!

最佳答案

"What am I doing wrong?"



您的 XPath 没有考虑默认命名空间,这里声明的没有前缀:xmlns="http://schemas.xmlsoap.org/wsdl/" . (参见this MSDN article 中的 默认命名空间 部分了解更多详细信息)。

"What can I do to fix it?"



使用 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)

关于VB.Net XmlDocument.SelectNodes(xPath) : unable to query WSDL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34868906/

相关文章:

php - 如何使用 PHP 将 XPath 表达式作为 XSL 参数传递?

python - 在 Webelement 中通过 xpath 查找 Selenium

selenium - 如何在 Selenium 中找到具有多个类的元素

c++ - 我们可以像在 C++ 中一样使用十六进制字节和字符吗?

javascript - 意外的标记,-asp 值显示为未定义

c# - 使用 C# 查找具有路径和 id 属性值的单个节点

c# - XmlNodeReader 返回 {无}

c# - 如何忽略 XML 文件中的前导空格?

asp.net - 为什么使用 HTML5 uploader 时 Request.Files.Count 有时为 0?

vb.net - 进程启动和应用程序运行缓慢导致的错误