0

所以我在 VB.NET 中编写了这个程序(很好地接受了代码并对其进行了修改),它从 POST 中获取信息。将该信息存储到一个值中。现在正在尝试解析该值,但我无法通过。无论如何,任何人都可以提供一些我可以遵循的示例代码?

注意: XML 来自 URL POST。这是一个片段:

Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Xml

Module Module1
    Public Class WebRequestPostExample
        Public Shared Sub Main()
            ' Create a request using a URL that can receive a post.
            Dim request As WebRequest = WebRequest.Create("https://quickvin.carfax.com/1 ")
            ' Set the Method property of the request to POST.
            request.Method = "POST"
            ' Create POST data and convert it to a byte array.
            Dim postData As String

            postData = "<carfax-request>"
            postData = postData & "<license-plate>HSM2688</license-plate>"
            postData = postData & "<state>PA</state>"
            postData = postData & "<vin></vin>"
            postData = postData & "<product-data-id>5A5AE0DA8BC016CF</product-data-id>"
            postData = postData & "<location-id>CARFAX</location-id>"
            postData = postData & "</carfax-request>"

            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
            ' Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded"
            ' Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length
            ' Get the request stream.
            Dim dataStream As Stream = request.GetRequestStream()
            ' Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length)
            ' Close the Stream object.
            dataStream.Close()
            ' Get the response.
            Dim response As WebResponse = request.GetResponse()
            ' Display the status.
            Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
            ' Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access.
            Dim reader As New StreamReader(dataStream)
            ' Read the content.
            Dim responseFromServer As String = reader.ReadToEnd()
            ' Display the content.
            Console.WriteLine(responseFromServer)


            Try
                Dim m_xmld As XmlDocument
                Dim m_nodelist As XmlNodeList
                Dim m_node As XmlNode
                'Create the XML Document
                m_xmld = New XmlDocument()
                'Load the Xml file
                XDocument.Load("https://quickvin.carfax.com/1") 'something keeps failing at this point
                'Get the list of name nodes 
                m_nodelist = m_xmld.SelectNodes("/request-info/")
                'Loop through the nodes
                For Each m_node In m_nodelist
                    'Get the Gender Attribute Value
                    Dim licenseplateAttribute = m_node.Attributes.GetNamedItem("license-plate").Value
                    'Get the firstName Element Value
                    Dim locationidValue = m_node.ChildNodes.Item(0).InnerText
                    'Get the lastName Element Value
                    Dim stateValue = m_node.ChildNodes.Item(1).InnerText
                    'Get the lastName Element Value
                    Dim vinValue = m_node.ChildNodes.Item(2).InnerText
                    'Write Result to the Console
                    Console.Write("Gender: " & licenseplateAttribute _
                      & " FirstName: " & locationidValue & " LastName: " _
                      & stateValue)
                    Console.Write(vbCrLf)
                Next
            Catch errorVariable As Exception
                'Error trapping
                Console.Write(errorVariable.ToString())
            End Try
            ' Clean up the streams.
            reader.Close()
            dataStream.Close()
            response.Close()
        End Sub
    End Class
End Module
4

2 回答 2

1

一种选择是使用XDocument.Load。其中一个重载是流,我怀疑您正在从通话中恢复过来。例如

XDocument.Load(yourStream)

..只要您有有效的 XML。

于 2013-05-29T20:14:24.337 回答
0

类似于 George 的回答,但这次使用XmlDocument类与使用您可能不需要的 LINQ 技术/功能的 XDocument 类:

Dim x As New XmlDocument()
x.Load("http://localhost/TheXMLFileToLoad.xml")

但是在您的情况下,您可能在 POST 操作之后已经在内存中拥有 XML 的流或字符串。在这种情况下,您可以加载 POST 流结果:

x.Load(yourStream)

或者,如果您的 POST 结果位于String

x.LoadXml(postXMLString)
于 2013-05-29T21:46:53.543 回答