I have an xml with multiple nodes of the same name
<?xml version="1.0" encoding="UTF-8"?>
<Versions>
<Version>
<Trunk>GapGun Software Version 7.1</Trunk>
<Branch>.142</Branch>
<Branch>.145</Branch>
<Branch>.148</Branch>
<Branch>.153</Branch>
<Branch>.176</Branch>
</Version>
<Version>
<Trunk>GapGun Software Version 7.2</Trunk>
<Branch>.142</Branch>
<Branch>.145</Branch>
<Branch>.148</Branch>
<Branch>.153</Branch>
<Branch>.176</Branch>
</Version>
</Versions>
I need to populate a combo box when filtered using the Trunk as a query so far i have this code
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim xelement As XElement = XElement.Load("F:\Test.xml")
Dim Versions As IEnumerable(Of XElement) = xelement.Elements()
For Each Version In Versions
Console.WriteLine(Version.Element("Trunk").Value)
ComboBox1.Items.Add(Version.Element("Trunk").Value)
Next Version
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim xelement As XElement = XElement.Load("F:\Test.xml")
Dim name =
From nm In xelement.Elements("Version")
Where CStr(nm.Element("Trunk")) = ComboBox1.Text
Select nm
For Each xEle As XElement In name
Console.WriteLine(xEle)
ComboBox2.Items.Add(xEle.Element("Branch").Value)
Next xEle
End Sub
End Class
This works but only returns the first branch please help, i am a complete novice!
Try following :
Your problem is that you using method
XElement.Element
which will return one(first) element of given name.From docs: Gets the first (in document order) child element with the specified XName.
You need change code to loop all "Branch" elements and you can add all of them by using
ComboBox.Items.AddRange
methodTo make code little bid simpler and load files only ones - you can introduce
Version
class which contains all required data.Then you don't need to search for correct trunk and will use already existing branches.
Then when user selects trunk populating branches combobox will be much simpler