解析 XML , VB.Net

Parsing XML , VB.Net

我通常解析以下响应:

<response>
<status>PURCHASED</status>
<amount>£1.00</amount>
<redirecturl>www.test.com</redirecturl>
</response>

我这样解析,效果很好:

xml.LoadXml(sresult)
                If xml.SelectSimpleNode("response/status").InnerText = "PURCHASED" Then
                    app.Outcome.RedirectURL = xml.SelectSimpleNode("response/redirecturl").InnerText

                    lendertier.LenderComm = CDec(xml.SelectSimpleNode("response/amount").InnerText)
                    AcceptLead()
                    Return True

我在尝试解析以下内容时遇到了一个主要问题:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><PostApplication_V4Response xmlns="http://test.org/"><PostApplication_V4Result>&lt;QuiddiSerivce&gt;
&lt;Application&gt;
&lt;Status&gt;PURCHASED&lt;/Status&gt;    &lt;RedirectURL&gt;https://www.test.com/Application/WebServiceRedirect.aspx?ID=123456767&lt;/RedirectURL&gt;
&lt;Value&gt;1.0400&lt;/Value&gt;
&lt;LenderID&gt;test_83&lt;/LenderID&gt;
&lt;ID&gt;e6965968-4725-44ec-8dc2-47072767721f&lt;/ID&gt;
&lt;/Application&gt;
&lt;/QuiddiSerivce&gt;
</PostApplication_V4Result></PostApplication_V4Response></soap:Body></soap:Envelope>

我只需要解析并获取“ Status 、 Value 和 redirecturl 的值

您必须使用名称空间; select编码节点的内容;而不是将编码节点的内容加载到另一个 xml 文档然后你可以 select Status/Value/RedirectUrl.

'Example - how to get to "Status"
Dim doc = New XmlDocument()
doc.LoadXml(xml)
Dim manager = New XmlNamespaceManager(doc.NameTable)
manager.AddNamespace("test", "http://test.org/")
Dim node = doc.SelectSingleNode("//test:PostApplication_V4Response", manager)
Dim innerDoc = New XmlDocument()
innerDoc.LoadXml(node.InnerText)
Dim statusNode = innerDoc.SelectSingleNode("//Status")
sresult = sresult.Replace("""", String.Empty)
            If sresult.Contains("Status:Accepted") Then
                Dim parts = sresult.Replace("{", String.Empty).Replace("}", String.Empty).Split(",")
                For i As Int16 = 0 To parts.Length - 1
                    If parts(i).StartsWith("Detail") Then
                        (yourclass).RedirectURL = parts(i).Substring(7)

                    End If
                    If parts(i).StartsWith("Price") Then
                        (yourclass).LenderComm = CDec(parts(i).Substring(6))
                    End If