Get Value Of Node in XML Response From HTTP Request in

This one took me a while to do. Basically it is in a memory stream and we are trying to get a specific value of a node in the response.

I used a disk file to move the Xml to a memory stream for completeness. If you place 2 text boxes and 2 buttons on a form and copy this code into the code file of the form and place the Xml file you posted in the bin/debug directory it should work fine. Click Button1 to load the XML document, place the XPath of the element you want into txtXpath text box and click Button2 get results in text box txtResults
TextBox names
txtXpath  = The Xpath to the element you want
txtResults = The InnerText of that element
If there is something that has multiple nodes then you will need to use the method xDoc.GetElementsByTagName so that you can get a list of nodes. This wasn’t the exact code I ended up using but this gets the point across. Hope it helps someone.

Imports System.Xml
Imports System.IO
Public Class Form1
    Private memStream As MemoryStream
    Private xDoc As New XmlDocument
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' This section of code is just to get the XML into memory using a memory stream
        ' Create a stream reader to read document into program
        Dim sr As New StreamReader("XmlInMemory.xml")
        ' Read document into string variable
        Dim xmlStr As String = sr.ReadToEnd()
        ' Create a byte array of characters from the string data/document
        Dim xmlBytesIn() As Byte = System.Text.Encoding.Default.GetBytes(xmlStr)
        ' Create the memory stream
        memStream = New MemoryStream()
        ' Write byte array into memory using memory stream
        memStream.Write(xmlBytesIn, 0, xmlBytesIn.Length)
        ' make sure that all data has been written to memory
        ' This section of code is used to load the XML document from a MemoryStream
        ' Reposition file pointer to the begining of the file
        memStream.Position = 0
        ' Create a buffer to hole the data from the MemoryStream
        Dim XmlBytesOut(CInt(memStream.Length)) As Byte
        ' Read the data from the system memory into the buffer
        memStream.Read(XmlBytesOut, 0, CInt(memStream.Length))
        ' Close the buffer
        ' Convert the byte array into a string
        Dim XmlData As String = System.Text.Encoding.Default.GetString(XmlBytesOut)
        ' Load the XML file into an XmlDocument
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ' Xpath string to the transaction-number = "transaction-response/transaction-number"
        Dim xNode As XmlNode = xDoc.SelectSingleNode(txtXpath.Text)
        txtResults.Text = xNode.InnerText
    End Sub
End Class

Technorati Tags: ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Windows Live Tags:,csharp,.NET Framework,HTTP,XML,Value,Node,Response,Request,memory,disk,text,directory,XPath,element,InnerText,GetElementsByTagName,System,MemoryStream,XmlDocument,StreamReader,XmlInMemory,ReadToEnd,Byte,GetBytes,Write,Length,Flush,Reposition,pointer,Position,XmlBytesOut,XmlData,LoadXml,transaction,XmlNode,SelectSingleNode,nodes

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: