Thomas Dutoit
Thomas Dutoit

Reputation: 61

Get data from XML string in VB.net

I'm having an XML sting looking like this:

<?xml version="1.0" encoding="UTF-8"?>
<SR Workstation="0014" Status="Active">
 <OS>
  <Identification Hardware="DELL" SerialNumber="123456789">Confirmed</Identification>
  <CN> EXTERN</CN>
  <CV>1.1.2.45</CV>
  <TS>Idle</TS>
  <TSS>Ok</TSS>
  <ReaderStatus Reader="Icc">Enabled</ReaderStatus>
  <ReaderStatus Reader="MS">Enabled</ReaderStatus>
  <LPS>Unavailable</LPS>
 </OS>
</SR>

I can get the Workstation data and the Status Data with this script in VB.net

Dim sr As New System.IO.StringReader(l_Result.XMLData)
        Dim doc As New Xml.XmlDocument
        doc.Load(sr)
        Dim reader As New Xml.XmlNodeReader(doc)
        While reader.Read()
            Select Case reader.NodeType
                Case Xml.XmlNodeType.Element
                    If reader.Name = "SR" Then
                        If reader.GetAttribute("WorkStation") = "0014" Then
                            txtStatus.Text = reader.GetAttribute("Status")
                            txtSerial.Text = reader.GetAttribute("Identification/SerialNumber")
                            txtHardware.Text = reader.GetAttribute("Identification/Hardware")
                            lblConfirmed.Text = reader.GetAttribute("Identification")
                        End If
                    End If

            End Select
        End While

But getting the values 123456789 (SerialNumber), DELL (Hardware) and Confirmed isn't working.

Can someone help me? I aint getting errors but my textboxes and label remain empty.

Upvotes: 1

Views: 8286

Answers (3)

Slai
Slai

Reputation: 22866

Using VB.Net XML Axis Properties:

Dim x = System.Xml.Linq.XElement.Parse(l_Result.XMLData), e = x...<Identification>
txtStatus.Text = x.@Status
txtSerial.Text = e.@SerialNumber
txtHardware.Text = e.@Hardware
lblConfirmed.Text = e.Value

To loop over elements:

For Each e In x...<ReaderStatus>
    Debug.Print(e.Value)
    Debug.Print(e.@Reader)
Next

Upvotes: 3

Enigmativity
Enigmativity

Reputation: 117029

You need to use Linq to XML. Then your code is easy:

Dim doc = XDocument.Parse(l_Result.XMLData)

txtStatus.Text = doc.Root.Attribute("Status").Value
txtSerial.Text = doc.Root.Element("OS").Element("Identification").Attribute("SerialNumber").Value
txtHardware.Text = doc.Root.Element("OS").Element("Identification").Attribute("Hardware").Value
lblConfirmed.Text = doc.Root.Element("OS").Element("Identification").Value

Upvotes: 1

jdweng
jdweng

Reputation: 34421

You can use xml linq to get descendants :

Imports System.Xml
Imports System.Xml.Linq

Module Module1

    Sub Main()
        Dim doc As New Xml.XmlDocument
        doc.Load(sr)
        Dim reader As New Xml.XmlNodeReader(doc)
        While Not reader.EOF
            If reader.Name <> "SR" Then
                reader.ReadToFollowing("SR")
            End If
            If Not reader.EOF Then
                Dim sr As XElement = XElement.ReadFrom(reader)
                If CType(sr.Attribute("WorkStation"), String) = "0014" Then
                    txtStatus.Text = reader.GetAttribute("Status")

                    txtSerial.Text = CType(sr.Descendants("SerialNumber").FirstOrDefault(), String)
                    txtHardware.Text = CType(sr.Descendants("Hardware").FirstOrDefault(), String)
                    lblConfirmed.Text = CType(sr.Descendants("Identification").FirstOrDefault(), String)

                    Exit While
                End If
            End If
        End While

    End Sub

End Module

Upvotes: 0

Related Questions