473,749 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I process XML in Visual Basic?

TXShooter
6 New Member
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
3 1720
Rabbit
12,516 Recognized Expert Moderator MVP
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
TXShooter
6 New Member
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. :)
Jan 8 '13 #3
Rabbit
12,516 Recognized Expert Moderator MVP
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
4376
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 gives me an error from microsoft's error report and asks me to send or not send the error, i chose and then it quits. The error report won't let me copy the data, but i gather it's pretty useless information unless you have the source code to visual...
21
12210
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 written to a single interface, but support many different manufacturers' barcode scanning devices. I do not want to be tied to one manufacturers' software interfaces. - Must support use of the scanner from Visual Basic, and ideally from C/C++ and...
1
4148
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 asphalt drum operations. The architecture for this system is an Allen-Bradley SLC 5/04 PLC talking to
2
3602
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 Visual Basic .net standard 2003. Not installing Visual Basic at the WinXP SP1A level - was the only way I found that I could install WinXPSP2. My computer was purchased with WinXP SP1 Professional. I also have Norton Internet Security and Norton...
4
1729
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, - Programming Visual Basic by Balena (MS Press) and - Visual Basic 2005 by Willis (WROX), but they don't go into the forms design aspects and describing the various controls at all. What bookscan I get that will cover that?
97
5534
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 the first place. Anyone else heard about this development? The Master
4
3096
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 applications into Visual Basic.Net. My managers main thought is that Visual Basic 6 is (or has!) stopped being supported by Microsoft.
8
2862
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 elements of VB.net....so that you were confident to write a application from scratch. This wouldnt necessarily
3
8110
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 to then launch a vbscript, oe even a batch file when it is clicked? Thanks.
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.