如何解析WebResponse XML以使用VB.NET获取特定节点?

时间:2016-07-20 16:13:13

标签: xml vb.net soap

我正在尝试连接到网站并阅读返回的XML。这是我第一次尝试使用SOAP。 问题是我可以返回XML,但需要解析它以获取值。

这是我的代码,它将返回响应:

Dim request As HttpWebRequest = CreateWebRequest()
Dim soapEnvelopeXml As New XmlDocument()

Try
    soapEnvelopeXml.LoadXml("<?xml version=""1.0"" encoding=""utf-8""?>" & vbCr & vbLf _
                            & "           <soap:Envelope " _
                            & "           xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " _
                            & "           xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" " _
                            & "           xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" & vbCr & vbLf _
                            & "             <soap:Body>" & vbCr & vbLf _
                            & "               <AccountLogin xmlns=""http://api.filesanywhere.com/"">" & vbCr & vbLf _
                            & "                 <APIKey>" & sAPIKey & "</APIKey>" & vbCr & vbLf _
                            & "                 <OrgID>" & iOrgID & "</OrgID>" & vbCr & vbLf _
                            & "                 <UserName>" & sUserName & "</UserName>" & vbCr & vbLf _
                            & "                 <Password>" & sPassword & "</Password>" & vbCr & vbLf _
                            & "                 <AllowedIPList>" & sAllowedIPList & "</AllowedIPList>" & vbCr & vbLf _
                            & "                 <ClientEncryptParam>" & sClientEncryptParam & "</ClientEncryptParam>" & vbCr & vbLf _
                            & "            </AccountLogin>" & vbCr & vbLf _
                            & "            </soap:Body>" & vbCr & vbLf _
                            & "            </soap:Envelope>")

    Using stream As Stream = request.GetRequestStream()
        soapEnvelopeXml.Save(stream)
    End Using

    Using response As WebResponse = request.GetResponse()
        Using rd As New StreamReader(response.GetResponseStream())
            Dim soapResult As String = rd.ReadToEnd()
            Form1.TextBox1.Text = soapResult.ToString()
        End Using
    End Using

Catch ex As Exception
    MsgBox(ex.Message)
End Try

这是SOAP响应: SOAPResponse

我想要做的是将Token和ErrorMessage节点值分配给文本框。

我尝试过在网站上找到的这种方法,但它对我不起作用。我在textBox1.Text之后立即添加了我的代码块。

Dim doc As New XmlDocument
doc.LoadXml(soapResult)
Dim nsm = New XmlNamespaceManager(doc.NameTable)
nsm.AddNamespace("a", "http://api.filesanywhere.com/")
TextBox2.Text = doc.SelectSingleNode("/a:AccountLoginResult/a:LoginResult/a:Token", nsm).InnerText

非常感谢任何帮助。

0 个答案:

没有答案