Extract and process XML data from a string

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

    Extract and process XML data from a string

    I'm trying to process the returndata from this device:


    My code is thus:
    Code:
    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 and update the webrelay state and input text boxes
              Dim returndata As String = Encoding.ASCII.GetString(bytes)
    
              'Parse out the DAQ state
              Dim array1 As Char() = returndata.ToCharArray()
    
    Whereas "array1" contains the string returned from the device. However, that string actually contains an XML response, and try as I might, I can't seem to find how to process it AS XML. The following is exactly the data being sent from the device:
    "<?xml version="1.0" encoding="utf-8"?>
    <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>"
    
    For now, I'm processing the string as follows:
              Dim PictureBoxList() As PictureBox = {In1_Picturebox, In2_Picturebox, In3_Picturebox, In4_Picturebox, In5_Picturebox}
              Dim k As Int16 = 67
              For i = 0 To PictureBoxList.Length - 1
                If array1(k) = "1" Then
                  PictureBoxList(i).Image = My.Resources.Green_LED___On
                Else
                  PictureBoxList(i).Image = My.Resources.Green_LED___Off
                End If
                k = k + 30
              Next
    k is where each of the values reside within the string.


    Problem: How do I process it as actual XML Data, and use that to change the state of my pictureboxes?
    Last edited by Rabbit; Jan 3 '13, 01:25 AM. Reason: Please use code tags when posting code.
  • TXShooter
    New Member
    • Jan 2013
    • 6

    #2
    Code tags weren't available for my first post. Sorry.

    Comment

    • isaaccw
      New Member
      • Jan 2013
      • 2

      #3
      Hi TXShooter. I actually work for ControlByWeb and saw this post. I want to help where I can. You may want to check out this tutorial: http://msdn.microsoft.com/en-us/libr...(v=vs.95).aspx

      Hopefully this will help you troubleshoot your code.

      Comment

      • TXShooter
        New Member
        • Jan 2013
        • 6

        #4
        Thanks Isaac. I ended up going about it the 'semi-hard way' on this particular program. For now, this works, but I would LOVE to pick your brain about other aspects of both the DAQ and the Quad-Relay. Is there any way we can get together to discuss?

        Code:
                  
                  'Parse out the DAQ state
                  Dim array1 As Char() = returndata.ToCharArray()
                  Dim source As Char() = array1
                  Dim TextBoxString As String = ""
                  Dim DAQDoc As New XmlDocument
                  Dim DAQNodes As XmlNodeList
                  Dim DAQNode As XmlNode
                  DAQDoc.LoadXml(source)
                  DAQNodes = DAQDoc.GetElementsByTagName("datavalues")
                  Dim in1variable As String = ""
                  Dim in2variable As String = ""
                  Dim in3variable As String = ""
                  Dim in4variable As String = ""
                  Dim in5variable As String = ""
                  Dim in1value As String = ""
                  Dim in2value As String = ""
                  Dim in3value As String = ""
                  Dim in4value As String = ""
                  Dim in5value As String = ""
                  Dim inVarArray() As String = {in1variable, in2variable, in3variable, in4variable, in5variable}
                  Dim inValArray() As String = {in1value, in2value, in3value, in4value, in5value}
                  Dim m As Integer = 0
                  For Each DAQNode In DAQNodes
                    Dim baseDAQNodes As XmlNodeList
                    Dim bFirstInRow As Boolean
                    baseDAQNodes = DAQNode.ChildNodes
                    bFirstInRow = True
                    'Load the varible names and values
                    For Each baseDAQNode As XmlNode In baseDAQNodes
                      TextBoxString = TextBoxString + (baseDAQNode.Name & ": " & baseDAQNode.InnerText) + vbCr & vbLf
                      inVarArray(m) = baseDAQNode.Name
                      inValArray(m) = baseDAQNode.InnerText
                      m = m + 1
                      If m = 5 Then
                        Exit For
                      End If
                    Next
                  Next

        Comment

        • isaaccw
          New Member
          • Jan 2013
          • 2

          #5
          That's great to hear that you got it working. Definitely feel free to contact our support team by phone (1-435-590-5999) or email (support@Contro lByWeb.com) to help you out with some more specific questions about any of our products.

          Also, on our website you can download any of the product manuals to help in answering any questions you might have. The manual for the WebRelay-Quad is located here: www.controlbywe b.com/webrelay-quad/downloads.html

          Looking forward to hearing from you.

          Comment

          Working...