473,378 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How do I process XML in Visual Basic?

TXShooter
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.

3 1710
Rabbit
12,516 Expert Mod 8TB
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
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
12,516 Expert Mod 8TB
Not a problem, good luck with your project.
Jan 8 '13 #4

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

Similar topics

10
by: John | last post by:
I have a problem, it's not with any code I have because... there is no code. When I run a blank visual basic 6 form, it opens up just fine. When I add a text box, a caption, and a button... it...
21
by: CHANGE username to westes | last post by:
What are the most popular, and well supported, libraries of drivers for bar code scanners that include a Visual Basic and C/C++ API? My requirements are: - Must allow an application to be...
1
by: Robin Powers | last post by:
PROCESS CONTROLS/VISUAL BASIC PROGRAMMERS SALARY: TO $100,000 LOCATION: LOS ANGELES AREA Our Client has recently acquired process automation systems used to monitor and control their...
2
by: Ralph | last post by:
I used to have Visual Basic .net std. 2003 installed on WinXP SP1A. But I found it too hard to upgrade WinXP to SP2. Now, I do have WinXP SP2 installed, but I am having problems installing...
4
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, ...
97
by: Master Programmer | last post by:
An friend insider told me that VB is to be killled off within 18 months. I guess this makes sence now that C# is here. I believe it and am actualy surprised they ever even included it in VS 2003 in...
4
by: Chris Asaipillai | last post by:
Hi there My compay has a number of Visual Basic 6 applications which are front endeed onto either SQL Server or Microsoft Access databases. Now we are in process of planning to re-write these...
8
by: Chris Asaipillai | last post by:
Hi there I have some questions for those experienced Visual Basic 6 programmers out there who have made the transition from VB6 to Vb.net. How long did it take you to learn at least the basic...
3
by: mrmwalter | last post by:
Hi, I need to create a GUI that has a form, on the form it asks for username. I therefore decided to use Visual Basic (not much experience_ On the form will be a button, how do I get this button...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.