2015-09-29 34 views
0

我回來從Web服務下面的XML和我試圖選擇ttContext節點,但具體而言,我需要解析contextGroup = pcSessionID contextValue節點。問題XmlNode.SelectNodes

我想我會首先確保我得到的ttContext節點,然後從那裏工作修改我的代碼,但是當我運行下面的代碼時,我的節點列表包含0個節點。任何人都能帶領我走向正確的方向嗎?

昏暗xmlDoc中作爲XmlDocument的=新的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> 

昏暗根作爲XmlNode的= xmldoc.DocumentElement Dim nodelist As XmlNodeList = root.SelectNodes(「/ dsContext/ttContext」)

回答

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 
 
​

+0

對不起,我沒有迴應的時候了,但我沒有得到您的回覆我的電子郵件通知。我無法使用xml ling,因爲我堅持使用visual studio 2012以及任何暗示的默認.net框架版本。 – user313714

+0

Visual Studio 2012包含Linq(不是'g')XML。庫沒有默認設置。我在家中使用了許多不同的PC並且工作(VS2008和VS 2010),每臺計算機都有自己的默認庫。即使在不同計算機上的同一版本的VS也有不同的默認值。 IO庫永遠不會添加到模塊中,而且經常使用。你的意思是你從來不使用IO庫嗎?標準XML也不會添加到模塊中。而不是添加using語句使用System.Xml.Linq。 using語句只是創建一個快捷方式,因此您不必添加完整的庫名稱。 – jdweng