簡體   English   中英

通過VB.NET解析XML

[英]Parsing XML Parsing through VB.NET

所以我用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

一種選擇是使用XDocument.Load 重載之一是流,我懷疑您會從呼叫中恢復過來。 例如

XDocument.Load(yourStream)

..只要您具有有效的XML。

與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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM