1

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.

1
  • 1
    I'm not very familiar with XmlNodeReader (I much prefer System.Xml.Linq.XDocument) but I have a feeling you cannot attempt to traverse to a child node's attribute that way. Or, if you can, you would need to include "OS" in the path to "Identification". Commented Jun 3, 2018 at 11:53

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Hey @Slai , This is working, But i have more then one ReaderStatus, how can i perform a while or for each loop ??
1

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

4 Comments

Still nothing is happening
@ThomasDutoit - Please post your actual XML then. I tested against your sample - assuming it had at least one enclosing node - and it worked just fine for me.
@ThomasDutoit - Is there an XML namespace defined?
@ThomasDutoit - I updated my code to suit your real XML. Tested and working.
0

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

3 Comments

I changed From : Identification/Hardware To : Hardware
ok, but it still says there is an error on sr.Descendants There is something wrong there. Didn't know that getting a XML value was this difficult
There could be a few things causing the error. I changed the code to the way I always do these loops. See if this solves the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.