Quantcast
Viewing all articles
Browse latest Browse all 15801

Retrieve selectedvalue from listbox/xml file

Hello,
I haven't been working on visual basic very long but I'm running into a problem in my code.

I have a button that reads part of an xml file into a listbox.

In addition, I want to build a function that does the following:

Select a wine from the Listbox. then all data from that specific wine has to be read from the xml file and then this data has to be split into different text boxes.

"Kind" should go to the Textbox tBKind, "Name" to Textbox tbName etc.

I'm a bit stuck on this part. I thought I could use a Split method but I think I'm doing something wrong here. does anyone have any advice for me?

The code is as follows:



The button to read the Name and Country fields from the XML file:


Code:

Private Sub WineListBarButton_Click(sender As Object, e As RoutedEventArgs)
        Dim xelement As XElement = XElement.Load("Assets/Wines.xml")
        Dim xWine As IEnumerable(Of XElement) = xelement.Elements()
        Dim queryData = From Wine In xWine.Descendants("Wijn")
                        Where (Wine.Attribute("Country")).Value <> ""
                        Select Country= Wine.Attribute("Country").Value,
                              Naam = Wine.Attribute("Name").Value
                        Order By (Country)

        For Each xWine In xWine
            WineListBox.Items.Add(xWine.@Country.PadRight(37, " ") & xWine.@Name.PadRight(30, " "))

        Next xWine

    End Sub


The Listbox to get the selected item from the listbox:


Code:

Private Sub WineListBox_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles WineListBox.SelectionChanged
        Dim xdoc As XDocument = XDocument.Load("Assets/Wines.xml")


        Dim queryData = From Wine In xdoc.Descendants("Wine")
                        Select Name = Wine.Attribute("Name").Value,
                  Country= Wine.Attribute("Country").Value,
                  Region= Wine.Attribute("Region").Value,
                  Kind= Wine.Attribute("Kind").Value,
                  Grape= Wine.Attribute("Grape").Value,
                  AlcoholPrct = Wine.Attribute("AlcoholPrct").Value


        For Each Wine In queryData

            'This is just a try
            TBKind.Text = WineListBox.SelectedItem.Split(Wine.Kind)

        Next

End Sub



A few lines from the XML file:

<Wines>
<Wine Name="Canapi Grillo" Country="Italie" Region="Sicilie" Kind="Wit" Grape="Grillo" AlcoholPrct="12"/>
<Wine Name="Miopasso Fiano" Country="Italie" Region="Sicilie" Kind="Wit" Grape="Fiano" AlcoholPrct="13"/>
<Wine Name="Elegance Sauvignon Blanc" Country="Frankrijk" Region="Pays d'Oc" Kind="Wit" Grape="Sauvignon Blanc" AlcoholPrct="11,5"/>
</Wines>



Thanks in advance for reading this and for any advice

Viewing all articles
Browse latest Browse all 15801

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>