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

Extract and process XML data from a string

TXShooter
P: 6
I'm trying to process the returndata from this device:
http://www.controlbyweb.com/support/five-input.html

My code is thus:
Expand|Select|Wrap|Line Numbers
  1. Public Class ControlByWebTester_Form
  2.   Private Sub Read_DAQ()
  3.     Dim tcpClient As New System.Net.Sockets.TcpClient()
  4.     Dim port As Integer
  5.     Dim ipAddr As String = Convert.ToString(IPAddr_TextBox.Text)
  6.  
  7.     Try
  8.       'Connect to DAQ
  9.       port = Convert.ToInt32(IPPort_TextBox.Text)
  10.       tcpClient.Connect(IPAddr_TextBox.Text.ToString(), port)
  11.  
  12.       If tcpClient.Connected Then
  13.  
  14.         Me.Pwr_Picturebox.Image = My.Resources.Red_LED___On
  15.  
  16.         'Create a network stream object
  17.         Dim netStream As NetworkStream = tcpClient.GetStream()
  18.  
  19.         'If we can read and write to the stream then do so
  20.         If netStream.CanWrite And netStream.CanRead Then
  21.  
  22.           'Send the on command to read status of DAQ
  23.           Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("GET /state.xml?noReply=0 HTTP/1.1" & vbCrLf & vbCrLf)
  24.           netStream.Write(sendBytes, 0, sendBytes.Length)
  25.  
  26.           'Get the response from DAQ
  27.           Dim bytes(tcpClient.ReceiveBufferSize) As Byte
  28.           netStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
  29.  
  30.           'Parse the response and update the webrelay state and input text boxes
  31.           Dim returndata As String = Encoding.ASCII.GetString(bytes)
  32.  
  33.           'Parse out the DAQ state
  34.           Dim array1 As Char() = returndata.ToCharArray()
  35.  
  36. 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:
  37. "<?xml version="1.0" encoding="utf-8"?>
  38. <datavalues>
  39. <input1state>0</input1state>
  40. <input2state>0</input2state>
  41. <input3state>0</input3state>
  42. <input4state>0</input4state>
  43. <input5state>0</input5state>
  44. <count1>0</count1>
  45. <count2>0</count2>
  46. <count3>0</count3>
  47. <count4>0</count4>
  48. <count5>0</count5>
  49. <powerupflag>1</powerupflag>
  50. </datavalues>"
  51.  
  52. For now, I'm processing the string as follows:
  53.           Dim PictureBoxList() As PictureBox = {In1_Picturebox, In2_Picturebox, In3_Picturebox, In4_Picturebox, In5_Picturebox}
  54.           Dim k As Int16 = 67
  55.           For i = 0 To PictureBoxList.Length - 1
  56.             If array1(k) = "1" Then
  57.               PictureBoxList(i).Image = My.Resources.Green_LED___On
  58.             Else
  59.               PictureBoxList(i).Image = My.Resources.Green_LED___Off
  60.             End If
  61.             k = k + 30
  62.           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?
Jan 2 '13 #1
Share this Question
Share on Google+
4 Replies


TXShooter
P: 6
Code tags weren't available for my first post. Sorry.
Jan 3 '13 #2

P: 2
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.
Jan 17 '13 #3

TXShooter
P: 6
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?

Expand|Select|Wrap|Line Numbers
  1.  
  2.           'Parse out the DAQ state
  3.           Dim array1 As Char() = returndata.ToCharArray()
  4.           Dim source As Char() = array1
  5.           Dim TextBoxString As String = ""
  6.           Dim DAQDoc As New XmlDocument
  7.           Dim DAQNodes As XmlNodeList
  8.           Dim DAQNode As XmlNode
  9.           DAQDoc.LoadXml(source)
  10.           DAQNodes = DAQDoc.GetElementsByTagName("datavalues")
  11.           Dim in1variable As String = ""
  12.           Dim in2variable As String = ""
  13.           Dim in3variable As String = ""
  14.           Dim in4variable As String = ""
  15.           Dim in5variable As String = ""
  16.           Dim in1value As String = ""
  17.           Dim in2value As String = ""
  18.           Dim in3value As String = ""
  19.           Dim in4value As String = ""
  20.           Dim in5value As String = ""
  21.           Dim inVarArray() As String = {in1variable, in2variable, in3variable, in4variable, in5variable}
  22.           Dim inValArray() As String = {in1value, in2value, in3value, in4value, in5value}
  23.           Dim m As Integer = 0
  24.           For Each DAQNode In DAQNodes
  25.             Dim baseDAQNodes As XmlNodeList
  26.             Dim bFirstInRow As Boolean
  27.             baseDAQNodes = DAQNode.ChildNodes
  28.             bFirstInRow = True
  29.             'Load the varible names and values
  30.             For Each baseDAQNode As XmlNode In baseDAQNodes
  31.               TextBoxString = TextBoxString + (baseDAQNode.Name & ": " & baseDAQNode.InnerText) + vbCr & vbLf
  32.               inVarArray(m) = baseDAQNode.Name
  33.               inValArray(m) = baseDAQNode.InnerText
  34.               m = m + 1
  35.               If m = 5 Then
  36.                 Exit For
  37.               End If
  38.             Next
  39.           Next
Jan 17 '13 #4

P: 2
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@ControlByWeb.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.controlbyweb.com/webrelay-quad/downloads.html

Looking forward to hearing from you.
Jan 17 '13 #5

Post your reply

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