How do I process XML in Visual Basic?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TXShooter
    New Member
    • Jan 2013
    • 6

    #1

    How do I process XML in Visual Basic?

    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?

    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
    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.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    If you have verified that array1 is returning correctly, then the problem is most likely line 69. It should probably be this:
    Code:
    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.

    Comment

    • TXShooter
      New Member
      • Jan 2013
      • 6

      #3
      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 "input1stat e: 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. :)
      Last edited by TXShooter; Jan 8 '13, 02:48 PM. Reason: Forgot to insert details.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Not a problem, good luck with your project.

        Comment

        Working...