0

So I wrote this program in VB.NET (well took code and modified it) and it grabs information from a POST. stores that information into a value. Now am trying to parse through that value and I cant get it through. Anyway anyone can provide some sample code I can follow?

Note: The XML is coming from a URL POST. Here's a snippet:

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
1
  • @Gilles: Always update your original question, should any relevant information appear. Especially for source code snippets, because those are not formatted/highlighted for better visibility. Notice how I updated your question just now. Commented May 30, 2013 at 0:14

2 Answers 2

1

One option would be to use XDocument.Load. One of the overloads is a stream, which I would suspect you are getting back from your call. e.g.

XDocument.Load(yourStream)

..so long as you have valid XML.

Sign up to request clarification or add additional context in comments.

5 Comments

so would i put the URL in the parenthesis or could i just put the value that represents the data fetched from the url? or it doesnt matter?
@Gilles: XDocument.Parse will allow you to feed the actual XML contents into it.
@Neolisk -- Sure, that's why I pointed out an overload is a stream. Usually, when you grab a web response, it comes back as a stream.
@George: My impression was that OP does not like streams, hence the comment. :) Also OP seems to have forgotten to +1 your answer after accepting, so here is a fix for ya!
thank you guys, ill try to post some of the code later it is rather lengthly ahah...well the XML stream is
0

Similar to George's answer, but this time using the XmlDocument class versus the XDocument class that is using LINQ technologies/capabilities that you may not need:

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

But in your case you probably already have the stream or a string of the XML already in memory following the POST operation. In that case you would do either to load the POST stream results:

x.Load(yourStream)

Or if your POST results are in a String:

x.LoadXml(postXMLString)

6 Comments

okay, for whatever reason I tried that, that's exactly how I had my syntax but for whatever reason it wouldn't except it :-/
What are the symptoms/errors when you attempt to load the XML? Looking at your code sample, I'm not sure why you are going back to the server with this line of code XDocument.Load("https://quickvin.carfax.com/1") This code is causing another trip to CarFax.com and since this request doesn't have the appropriate header fields, it fails with 404 error. You already have the XML response from the server. Replace that line of code with m_xmld.LoadXml(responseFromServer)
see I tried that, and it doesnt seem to be grabbing the file.
I ran your code and found a couple of things: 1) The m_xmld.LoadXml(responseFromServer) does load the response XML correctly. 2) An Exception is thrown on this line m_nodelist = m_xmld.SelectNodes("/request-info/") because you have a bad XPath. Replace it with m_nodelist = m_xmld.SelectNodes("/carfax-response/request-info"). 3) Your code for fetching the child fields of the request-info node have a few problems. 4) The license-plate is not an attribute, so this code Dim licenseplateAttribute = m_node.Attributes.GetNamedItem("license-plate").Value will throw an exception.
<continued from previous comment> Replace that code with Dim licenseplateAttribute = m_node.SelectSingleNode("license-plate").Value 5) The remaining code to retrieve the <location-id>, <state> and <vin> nodes is looking in the wrong position (now that the <license-plate> is in the 1st position. I recommend using the m_node.SelectSingleNode technique instead of relying on the order of the nodes in the XML file. Additionally, this makes for more readable code IMO.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.