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?
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.
input1state = 0
input2state = 0
input3state = 0
input4state = 0
input5state = 0
Can anyone help me out here?
Code:
Imports System.Net.Sockets
Imports System.Text
Imports System.Xml
Imports System.IO
Imports System.Net
Imports System.Xml.Xsl
Public Class ControlByWebTester_Form
Private Sub Read_DAQ()
Dim tcpClient As New System.Net.Sockets.TcpClient()
Dim port As Integer
Dim ipAddr As String = Convert.ToString(IPAddr_TextBox.Text)
Try
'Connect to DAQ
port = Convert.ToInt32(IPPort_TextBox.Text)
tcpClient.Connect(IPAddr_TextBox.Text.ToString(), port)
If tcpClient.Connected Then
Me.Pwr_Picturebox.Image = My.Resources.Red_LED___On
'Create a network stream object
Dim netStream As NetworkStream = tcpClient.GetStream()
'If we can read and write to the stream then do so
If netStream.CanWrite And netStream.CanRead Then
'Send the on command to read status of DAQ
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("GET /state.xml?noReply=0 HTTP/1.1" & vbCrLf & vbCrLf)
netStream.Write(sendBytes, 0, sendBytes.Length)
'Get the response from DAQ
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
netStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
'Parse the response
Dim returndata As String = Encoding.ASCII.GetString(bytes)
'Parse out the DAQ state
Dim array1 As Char() = returndata.ToCharArray() 'Used by different part of sub not shown
Dim source As Char() = array1
'The Stream's String contains (without whitespaces):
'<datavalues>
' <input1state>0<input1state/>
' <input2state>0<input2state/>
' <input3state>0<input3state/>
' <input4state>0<input4state/>
' <input5state>0<input5state/>
' <count1>0</count1>
' <count2>0</count2>
' <count3>0</count3>
' <count4>0</count4>
' <count5>0</count5>
' <powerupflag>1</powerupflag>
'</datavalues>
Dim LabelString As String = " "
Dim DAQDoc As New XmlDocument
Dim DAQNodes As XmlNodeList
Dim DAQNode As XmlNode
DAQDoc.LoadXml(source)
DAQNodes = DAQDoc.GetElementsByTagName("datavalues")
For Each DAQNode In DAQNodes
Dim baseDAQNodes As XmlNodeList
Dim bFirstInRow As Boolean
baseDAQNodes = DAQNode.ChildNodes
bFirstInRow = True
For Each baseDAQNode As XmlNode In DAQNodes
LabelString = LabelString + (baseDAQNode.Name & ": " & baseDAQNode.InnerText) + vbCr & vbLf
Next
Next
'Display the Variables and Values from the XML string,
Me.Label1.Text = LabelString
End If
'Close the connection
tcpClient.Close()
End If
Catch e As Exception
Me.Label1.Text = "An error has occurred."
'Disable the timer
Timer1.Enabled = False
End Try
End Sub
Private Indicator As Long = 0
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Read_DAQ()
Me.Label2.Text = "Reading DAQ#1"
End Sub
Private Sub DAQ1_Start_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DAQ1_Start_Button.Click
Timer1.Enabled = True
Timer1.Interval = 250
Me.Label2.Enabled = True
DAQ1_Start_Button.Enabled = False
DAQ1_Stop_Button.Enabled = True
IPAddr_TextBox.Enabled = False
IPPort_TextBox.Enabled = False
End Sub
Private Sub DAQ1_Stop_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DAQ1_Stop_Button.Click
IPAddr_TextBox.Enabled = True
IPPort_TextBox.Enabled = True
Me.Label2.Enabled = False
DAQ1_Start_Button.Enabled = True
DAQ1_Stop_Button.Enabled = False
Timer1.Stop()
End Sub
End Class
Comment