By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,285 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,285 IT Pros & Developers. It's quick & easy.

How do I process XML in Visual Basic?

TXShooter
P: 6
I've tried, and I've tried, and I've tried... I just can't seem to get a handle on parsing an XML stream from a device. It should be simple, but I MUST be missing something. All I want to do is to display the variables in the XML stream, and the values to those variables, in Me.Labelx.Text format so that my form looks like:

input1state = 0
input2state = 0
input3state = 0
input4state = 0
input5state = 0

Can anyone help me out here?

Expand|Select|Wrap|Line Numbers
  1. Imports System.Net.Sockets
  2. Imports System.Text
  3. Imports System.Xml
  4. Imports System.IO
  5. Imports System.Net
  6. Imports System.Xml.Xsl
  7.  
  8.  
  9. Public Class ControlByWebTester_Form
  10.   Private Sub Read_DAQ()
  11.     Dim tcpClient As New System.Net.Sockets.TcpClient()
  12.     Dim port As Integer
  13.     Dim ipAddr As String = Convert.ToString(IPAddr_TextBox.Text)
  14.  
  15.     Try
  16.       'Connect to DAQ
  17.       port = Convert.ToInt32(IPPort_TextBox.Text)
  18.       tcpClient.Connect(IPAddr_TextBox.Text.ToString(), port)
  19.  
  20.       If tcpClient.Connected Then
  21.  
  22.         Me.Pwr_Picturebox.Image = My.Resources.Red_LED___On
  23.  
  24.         'Create a network stream object
  25.         Dim netStream As NetworkStream = tcpClient.GetStream()
  26.  
  27.         'If we can read and write to the stream then do so
  28.         If netStream.CanWrite And netStream.CanRead Then
  29.  
  30.           'Send the on command to read status of DAQ
  31.           Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("GET /state.xml?noReply=0 HTTP/1.1" & vbCrLf & vbCrLf)
  32.           netStream.Write(sendBytes, 0, sendBytes.Length)
  33.  
  34.           'Get the response from DAQ
  35.           Dim bytes(tcpClient.ReceiveBufferSize) As Byte
  36.           netStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
  37.  
  38.           'Parse the response
  39.           Dim returndata As String = Encoding.ASCII.GetString(bytes)
  40.  
  41.           'Parse out the DAQ state
  42.           Dim array1 As Char() = returndata.ToCharArray()    'Used by different part of sub not shown
  43.           Dim source As Char() = array1
  44.           'The Stream's String contains (without whitespaces):
  45.           '<datavalues>
  46.           '  <input1state>0<input1state/>
  47.           '  <input2state>0<input2state/>
  48.           '  <input3state>0<input3state/>
  49.           '  <input4state>0<input4state/>
  50.           '  <input5state>0<input5state/>
  51.           '  <count1>0</count1>
  52.           '  <count2>0</count2>
  53.           '  <count3>0</count3>
  54.           '  <count4>0</count4>
  55.           '  <count5>0</count5>
  56.           '  <powerupflag>1</powerupflag>
  57.           '</datavalues>
  58.           Dim LabelString As String = " "
  59.           Dim DAQDoc As New XmlDocument
  60.           Dim DAQNodes As XmlNodeList
  61.           Dim DAQNode As XmlNode
  62.           DAQDoc.LoadXml(source)
  63.           DAQNodes = DAQDoc.GetElementsByTagName("datavalues")
  64.           For Each DAQNode In DAQNodes
  65.             Dim baseDAQNodes As XmlNodeList
  66.             Dim bFirstInRow As Boolean
  67.             baseDAQNodes = DAQNode.ChildNodes
  68.             bFirstInRow = True
  69.             For Each baseDAQNode As XmlNode In DAQNodes
  70.               LabelString = LabelString + (baseDAQNode.Name & ": " & baseDAQNode.InnerText) + vbCr & vbLf
  71.             Next
  72.           Next
  73.  
  74.           'Display the Variables and Values from the XML string, 
  75.           Me.Label1.Text = LabelString
  76.  
  77.         End If
  78.  
  79.         'Close the connection
  80.         tcpClient.Close()
  81.  
  82.       End If
  83.  
  84.     Catch e As Exception
  85.       Me.Label1.Text = "An error has occurred."
  86.       'Disable the timer
  87.       Timer1.Enabled = False
  88.     End Try
  89.   End Sub
  90.  
  91.   Private Indicator As Long = 0
  92.  
  93.   Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  94.  
  95.     Read_DAQ()
  96.     Me.Label2.Text = "Reading DAQ#1"
  97.  
  98.   End Sub
  99.  
  100.   Private Sub DAQ1_Start_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DAQ1_Start_Button.Click
  101.  
  102.     Timer1.Enabled = True
  103.     Timer1.Interval = 250
  104.     Me.Label2.Enabled = True
  105.     DAQ1_Start_Button.Enabled = False
  106.     DAQ1_Stop_Button.Enabled = True
  107.     IPAddr_TextBox.Enabled = False
  108.     IPPort_TextBox.Enabled = False
  109.  
  110.   End Sub
  111.  
  112.   Private Sub DAQ1_Stop_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DAQ1_Stop_Button.Click
  113.  
  114.     IPAddr_TextBox.Enabled = True
  115.     IPPort_TextBox.Enabled = True
  116.     Me.Label2.Enabled = False
  117.     DAQ1_Start_Button.Enabled = True
  118.     DAQ1_Stop_Button.Enabled = False
  119.     Timer1.Stop()
  120.  
  121.   End Sub
  122.  
  123. End Class
  124.  
I've asked for help on this before, but perhaps no one realized what it was that I was asking for??? I've gotten no answers to it other than, "Put code in code blocks." So, I ask it again with a cleaner looking code setup.
Jan 7 '13 #1

✓ answered by Rabbit

If you have verified that array1 is returning correctly, then the problem is most likely line 69. It should probably be this:
Expand|Select|Wrap|Line Numbers
  1. For Each baseDAQNode As XmlNode In baseDAQNodes 
But it's hard to say because you haven't actually described what the problem is, only that it's not working.

Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,392
If you have verified that array1 is returning correctly, then the problem is most likely line 69. It should probably be this:
Expand|Select|Wrap|Line Numbers
  1. For Each baseDAQNode As XmlNode In baseDAQNodes 
But it's hard to say because you haven't actually described what the problem is, only that it's not working.
Jan 7 '13 #2

TXShooter
P: 6
Holy C**p, Rabbit, that did it!

"source" contained the entire xml 'file' in a streaming string.

The problem was that "datavalues: 10000000001" was my output instead of "input1state: 0" (etc.). I needed to break down the string into appropriate variable/value segments.

However, it would seem that I'll need to go a completely different direction with my XML programming in order to process other devices. I'll need to figure out how to traverse from top-to-bottom the entire tree, get/store the variable names, and their values, from a program point of view. It would seem unwise to write a custom program just for the sake of reading each device I have.

Thank you very much, Rabbit. :)
Jan 8 '13 #3

Rabbit
Expert Mod 10K+
P: 12,392
Not a problem, good luck with your project.
Jan 8 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.