XmlNode.SelectNodes问题

时间:2015-09-29 21:16:19

标签: xml

我从Web服务中回到下面的xml,我尝试选择ttContext节点,但具体来说我需要解析contextGroup = pcSessionID的contextValue节点。

我想我首先要确保我到达ttContext节点,然后在那里工作修改我的代码,但是当我运行下面的代码时,我的节点列表包含0个节点。谁能引导我朝着正确的方向前进?

Dim xmldoc As XmlDocument = New XmlDocument

xmldoc.LoadXml(e.InnerXml)

e.InnerXml包含以下xml:

   <SOAP-ENV:Envelope
         xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <SOAP-ENV:Body>
           <apSrvResponse xmlns="urn:eTimeWS:eTimeWS">
              <result xsi:nil="true" />
              <dsContext>
                 <ttContext>
                    <contextGroup>PARAM</contextGroup>
                    <contextName>pcPassword</contextName>
                    <contextValue>admin</contextValue>
                    <contextOperator />
                    <contextType />
                 </ttContext>
                 <ttContext>
                    <contextGroup>PARAM</contextGroup>
                    <contextName>pcSessionID</contextName>
                    <contextValue>admin.8c4a11bf-a8e0-3e9e-df11-84e3cd76215d.bJZfvubladuiXKMj</contextValue>
                    <contextOperator />
                    <contextType />
                 </ttContext>
                 <ttContext>
                    <contextGroup>PARAM</contextGroup>
                    <contextName>pcUserID</contextName>
                    <contextValue>admin</contextValue>
                    <contextOperator />
                    <contextType />
                 </ttContext>
              </dsContext>
           </apSrvResponse>
        </SOAP-ENV:Body>
     </SOAP-ENV:Envelope>

Dim root As XmlNode = xmldoc.DocumentElement Dim nodelist As XmlNodeList = root.SelectNodes(&#34; / dsContext / ttContext&#34;)

1 个答案:

答案 0 :(得分:0)

有点复杂的命名空间。我使用XML Linq

Imports System.Xml
Imports System.Xml.Linq
Module Module1

    Sub Main()
        Dim input As String = _
       "<SOAP-ENV:Envelope" & _
         " xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/""" & _
         " xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""" & _
         " xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">" & _
        "<SOAP-ENV:Body>" & _
           "<apSrvResponse xmlns=""urn:eTimeWS:eTimeWS"">" & _
              "<result xsi:nil=""true"" />" & _
              "<dsContext>" & _
                 "<ttContext>" & _
                    "<contextGroup>PARAM</contextGroup>" & _
                    "<contextName>pcPassword</contextName>" & _
                    "<contextValue>admin</contextValue>" & _
                    "<contextOperator />" & _
                    "<contextType />" & _
                 "</ttContext>" & _
                 "<ttContext>" & _
                    "<contextGroup>PARAM</contextGroup>" & _
                    "<contextName>pcSessionID</contextName>" & _
                    "<contextValue>admin.8c4a11bf-a8e0-3e9e-df11-84e3cd76215d.bJZfvubladuiXKMj</contextValue>" & _
                    "<contextOperator />" & _
                    "<contextType />" & _
                 "</ttContext>" & _
                 "<ttContext>" & _
                    "<contextGroup>PARAM</contextGroup>" & _
                    "<contextName>pcUserID</contextName>" & _
                    "<contextValue>admin</contextValue>" & _
                    "<contextOperator />" & _
                    "<contextType />" & _
                 "</ttContext>" & _
              "</dsContext>" & _
           "</apSrvResponse>" & _
        "</SOAP-ENV:Body>" & _
     "</SOAP-ENV:Envelope>"

        Dim envelope As XElement = XElement.Parse(input)
        Dim ns As XNamespace = envelope.Name.Namespace

        Dim contextGroup As String = "pcSessionID"
        
        Dim results = envelope.Descendants().Where(Function(w) w.Name.LocalName = "ttContext" And _
                       w.Elements(w.Name.Namespace + "contextName").Where(Function(x) x.Value = contextGroup).Any()) _
                       .Select(Function(y) New With { _
                                  .contextGroup = y.Element(y.Name.Namespace + "contextGroup").Value, _
                                  .contextName = y.Element(y.Name.Namespace + "contextName").Value, _
                                  .contextValue = y.Element(y.Name.Namespace + "contextValue").Value, _
                                  .contextOperator = y.Element(y.Name.Namespace + "contextOperator").Value, _
                                  .contextType = y.Element(y.Name.Namespace + "contextType").Value _
                                   }).ToList()

    End Sub

End Module
​