How would I read XML data into an array? I have an XML sheet, and I want to read each of the values of a specific element into an array. So for each element in each data group I would have an array holding that data.

The idea is to be able to graph that data using the XML sheet, so that I dont have to keep recalculating the values everytime I want to graph.

Heres the XML code that Ive been trying to work with:

<MyData>
    <Gap>0.05</Gap>
    <Diameter>200.00</Diameter>
    <R2>2.54</R2>
    <VentDiamater>97.39999</VentDiamater>
    <R1>1.236979873</R1>
    <Height>0.00127</Height>
  </MyData>
  <MyData>
    <Gap>0.05</Gap>
    <Diameter>12.00</Diameter>
    <R2>0.1524</R2>
    <VentDiamater>5.844</VentDiamater>
    <R1>0.0742188</R1>
    <Height>0.00127</Height>
  </MyData>
  <MyData>
    <Gap>0.02</Gap>
    <Diameter>12.00</Diameter>
    <R2>0.1524</R2>
    <VentDiamater>5.844</VentDiamater>
    <R1>0.0742188</R1>
    <Height>0.000508</Height>
  </MyData>

and the code:

While reader.Read()
     Select Case reader.NodeType
        Case XmlNodeType.Element
              ' Keep track of the element that the user is on.
              s = reader.Name
        Case XmlNodeType.Text

             If s.Equals("Gap") Then

              Dim i As Integer = 0
              'Save the reader Value into an array with length i
              myItem(i) = reader.Value
              MsgBox(myItem(i))
              i += 1

          End If
     End Select
End While

So if you want to understand exactly what Im asking, I want an array to hold the values of Gap (theres 3 of them here), and then another array to hold the values of diameter, and so on.

Recommended Answers

All 6 Replies

Anyone have an opinion? I think I found a solution to this, but I want to make sure it will work.

Have you ever tried DaniWeb's Site Search?

Ok, here's a thread "parse XML in an array in vb.net console application" where I posted XML to array procedure. It should be easily modified for your needs.

Have you ever tried DaniWeb's Site Search?

Ok, here's a thread "parse XML in an array in vb.net console application" where I posted XML to array procedure. It should be easily modified for your needs.

I read through that code already, and I dont understand half of it, so yes I have tried the search already.

Besides, my application is not to compare an xml form with some values, I just need to be able to take the xml data and write it into some arrays.

I might have found a solution for this, I just wanted to get a second opinion about the question before I move on.

Thanks.

I have in my mind that you would take only Public Sub ImportXML(ByVal FileName As String, ByRef Names() As String, ByRef CountStart() As Integer, ByRef CountEnd() As Integer) routine and modify it for your purposes. It reads XML file to arrays and that's what you were looking for, am I right?

I dont understand half of it

It uses XPath to read XML file with three values. One is string type and the other two are integer types, and values are returned in arrays. There wasn't much comments in the code, I agree that.

Besides, my application is not to compare an xml form with some values, I just need to be able to take the xml data and write it into some arrays.

Yes. That's what the ImportXML is for.

I just wanted to get a second opinion about the question before I move on

Oh, what the heck. Here's the modified code

Public Sub ImportXML(ByVal FileName As String, _
  ByRef Gap() As Double, ByRef Diameter() As Double, _
  ByRef R2() As Double, ByRef VentDiameter() As Double, _
  ByRef R1() As Double, ByRef Height() As Double)
  ' 
  Dim FStream As FileStream
  Dim XReader As Xml.XmlTextReader
  Dim XDoc As XPath.XPathDocument
  Dim XNav As XPath.XPathNavigator
  Dim XIter As XPath.XPathNodeIterator
  Dim TempStr As String
  Dim TempDbl As Double
  Dim TempCount As Integer

  TempCount = 0
  FStream = New FileStream(FileName, FileMode.Open, FileAccess.Read)
  XReader = New Xml.XmlTextReader(FStream)
  XDoc = New XPath.XPathDocument(XReader)
  XNav = XDoc.CreateNavigator()

  '
  XNav.MoveToRoot()
  XIter = XNav.Select("descendant::Root/MyData")
  While XIter.MoveNext
    ReDim Preserve Gap(TempCount)
    ReDim Preserve Diameter(TempCount)
    ReDim Preserve R2(TempCount)
    ReDim Preserve VentDiameter(TempCount)
    ReDim Preserve R1(TempCount)
    ReDim Preserve Height(TempCount)
    ' Gap
    TempStr = XIter.Current.SelectSingleNode("descendant::Gap").Value
    If Double.TryParse(TempStr, TempDbl) Then
      Gap(TempCount) = TempDbl
    Else
      Gap(TempCount) = Double.NaN ' Double.NaN indicates invalid value
    End If
    ' Diameter
    TempStr = XIter.Current.SelectSingleNode("descendant::Diameter").Value
    If Double.TryParse(TempStr, TempDbl) Then
      Diameter(TempCount) = TempDbl
    Else
      Diameter(TempCount) = Double.NaN ' Double.NaN indicates invalid value
    End If
    ' R2
    TempStr = XIter.Current.SelectSingleNode("descendant::R2").Value
    If Double.TryParse(TempStr, TempDbl) Then
      R2(TempCount) = TempDbl
    Else
      R2(TempCount) = Double.NaN ' Double.NaN indicates invalid value
    End If
    ' VentDiameter
    TempStr = XIter.Current.SelectSingleNode("descendant::VentDiameter").Value
    If Double.TryParse(TempStr, TempDbl) Then
      VentDiameter(TempCount) = TempDbl
    Else
      VentDiameter(TempCount) = Double.NaN ' Double.NaN indicates invalid value
    End If
    ' R1
    TempStr = XIter.Current.SelectSingleNode("descendant::R1").Value
    If Double.TryParse(TempStr, TempDbl) Then
      R1(TempCount) = TempDbl
    Else
      R1(TempCount) = Double.NaN ' Double.NaN indicates invalid value
    End If
    ' Height
    TempStr = XIter.Current.SelectSingleNode("descendant::Height").Value
    If Double.TryParse(TempStr, TempDbl) Then
      Height(TempCount) = TempDbl
    Else
      Height(TempCount) = Double.NaN ' Double.NaN indicates invalid value
    End If
    TempCount += 1
  End While

  FStream.Close()

End Sub

if you want to try it out. You'll have to fix XML too. It should be similar to following

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <MyData>
    <Gap>0.05</Gap>
    <Diameter>200.00</Diameter>
    <R2>2.54</R2>
    <VentDiamater>97.39999</VentDiamater>
    <R1>1.236979873</R1>
    <Height>0.00127</Height>
  </MyData>
  <MyData>
    <Gap>0.05</Gap>
    <Diameter>12.00</Diameter>
    <R2>0.1524</R2>
    <VentDiamater>5.844</VentDiamater>
    <R1>0.0742188</R1>
    <Height>0.00127</Height>
  </MyData>
  <MyData>
    <Gap>0.02</Gap>
    <Diameter>12.00</Diameter>
    <R2>0.1524</R2>
    <VentDiamater>5.844</VentDiamater>
    <R1>0.0742188</R1>
    <Height>0.000508</Height>
  </MyData>
</Root>

I didn't actually test the code but I tested the original so I believe it works ;)

Tamir09>I want an array to hold the values of Gap..

Dear Tamir,
My little effort to read xml and store into an array is:

Dim reader As New XmlTextReader("c:\csnet\46\VBConsole\a1.xml")
        Dim s As String = ""
        Dim gap() As String
        Dim iGap As Integer
        While reader.Read()
            Select Case reader.NodeType
                Case XmlNodeType.Element
                    ' Keep track of the element that the user is on.
                    s = reader.Name
                Case XmlNodeType.Text

                    If s.Equals("Gap") Then
                        ReDim Preserve gap(iGap)

                        'Save the reader Value into an array with length i
                        gap(iGap) = reader.Value

                        iGap += 1

                    End If
            End Select
        End While
        For Each m As String In gap
            Console.WriteLine(m)
        Next

Thanks for the replies and helping me understand how to get it done.

On the other hand, the way I found it to be done is much shorter than all that, but thanks for the code suggestions anyways. :)

' Be sure to set a reference to System.Xml.Linq
        Dim doc As XElement = XElement.Load("testXML.xml")
        ' Load the data
        Dim allData = From d In doc...<MyData>

        ' Get the gaps information
        Dim gaps = (From g In doc...<Gap> Select g.Value).ToArray
        Dim diameters = (From d In doc...<Diameter> Select d.Value).ToArray
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.