-2

I am trying to work with a simple XML file and Visual Basic (2017).

The xml file is as below. I have a form setup with some text boxes. First box, is where the user will enter the value 1, 2 or 3. I then need to write the xml data from world name, size and population into 3 separate text boxes based on what is inputted into the first text box. Hope that makes sense.

<worlddetails>
<worlds>
<code>1</code>
<worldname>Planet 1>
<size>36000</size>
<population>34000000</population>
</worlds>  
<worlds>
<code>2</code>
<worldname>Planet 2>
<size>35000</size>
<population>24000000</population>
</worlds>  
<worlds>
<code>3</code>
<worldname>Planet 3>
<size>46000</size>
<population>14000000</population>
</worlds>    
</worlddetails>
  • What did you try? – litelite Oct 02 '17 at 14:36
  • Show the code you've tried and include specific details of where you're having an issue. – Andrew Mortimer Oct 02 '17 at 14:36
  • Welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour) and read [how to ask](https://stackoverflow.com/help/how-to-ask). – litelite Oct 02 '17 at 14:36
  • Also, `world details` and `world name` are not valid XML tag names (you cannot have a space in a tag name) and `Planet 1>` is not valid in xml (your end tag is malformed). That file is not even valid XML – litelite Oct 02 '17 at 14:37
  • That was just me typing. I'm struggling with this as I have seen articles on how to read and display the xml data, but I cant work out how to make it only read based on what it sees in that first text box. Excuse my English. – Bryan Sycamore Oct 02 '17 at 14:41
  • Start by fixing your file, and then you can use [this](https://stackoverflow.com/questions/4752796/how-to-read-xml-in-net). It's C# so you will have to adapt the syntax, but it's all the same classes and method. – litelite Oct 02 '17 at 14:42

1 Answers1

0

First of all, your XML is wrong. It should look like this:

<worlddetails>
  <worlds>
     <world name="1">
        <code>1</code>   
        <size>36000</size>
        <population>34000000</population>
     </world>
     <world name="2">
        <code>2</code>
        <size>35000</size>
        <population>24000000</population>
     </world>
     <world name="3">
        <code>3</code>
        <size>46000</size>
        <population>14000000</population>
     </world>
  </worlds>    
</worlddetails>

Once fixed, you can achieve your goal just using this function (not tested, you probably need to modify it) and passing the planet number as a parameter. It will collect the desired planet data into a string array which will be returned once the function ends. If the planet could not be found into the XML file, the function will return -1 into the array's first position.

Private Function getPlanetData(ByVal planetNo As Integer) As String()

        Dim planetData() As String = New String() {}
        Dim i As Integer = 0

        Dim doc As New Xml.XmlDocument

        Try
            doc.Load("yourXMLDocument.xml")
            Dim node As XmlNode = doc.SelectSingleNode("world[@name='" & planetNo & "']")
            If Not (node Is Nothing) Then
                Dim childList As Xml.XmlNodeList
                childList = node.ChildNodes
                ReDim planetData(childList.Count - 1)
                For Each child As XmlNode In childList
                    planetData(i) = child.InnerText
                    i += 1
                Next
            Else
                planetData(0) = -1
            End If

            Return planetData

        Catch ex As Exception

            planetData(0) = -1
            Return planetData
            MsgBox("There was an error when collecting data. ERROR: " & ex.message)

        End Try

    End Function
ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56