|
-
Jul 5th, 2021, 12:28 AM
#1
Thread Starter
New Member
Read XML file and insert into ListBox
Hello,
I'm trying to read an xml file and then put it in a ListBox in VB.net.
I just need the "Country" and "Name" columns.
But I only manage to read the entire XML file.
Does anyone have a tip on how I can read these 2 columns and get them into the ListBox?
Thanks in advance!
The XML file:
<Wines>
<Wine Name="Canapi Grillo" Country="Italie" Region="Sicilie" Soort="Wit" Druif="Grillo" AlcoholPrct="12"/>
<Wine Name="Miopasso Fiano" Country="Italie" Region="Sicilie" Soort="Wit" Druif="Fiano" AlcoholPrct="13"/>
<Wine Name="Elegance Sauvignon Blanc" Country= "Frankrijk" Region="Pays d'Oc" Soort="Wit" Druif="Sauvignon Blanc" AlcoholPrct="11,5"/>
</Wines>
The code:
Code:
Private Sub WijnLijstBarButton_Click(sender As Object, e As RoutedEventArgs)
Dim xdocument As XDocument = XDocument.Load("..\..\wine.xml")
Dim wines As IEnumerable(Of XElement) = xdocument.Elements()
For Each wine In wine
Listbox1.Items.Add(wine)
Next wine
-
Jul 5th, 2021, 01:27 AM
#2
Re: Read XML file and insert into ListBox
If you add the entire element to the ListBox then of course you are going to get the entire element. If you only want certain parts of that element then you have to write code to get those parts. Have you done any research on the XElement class, i.e. the type of your wine loop variable, to see what members it provides that could help you get the data you want? If you haven't at least read the documentation for that type then you haven't really tried.
You can go straight to that documentation by simply clicking on the type name in the code window and pressing F1. Apart from that, you can get to the documentation home page from the Help menu, although you do then have to click a few links. Once you get there the first time though, however you get there, you should be adding a favourite/bookmark to your browser, so you can always search the documentation first whenever you need to know something about a specific type or member. I lost count years ago of the number of questions I've answered on this site and others by simply doing that when the person asking the question could obviously have done the same and answered their own question. We're always here to help with the stuff you can work out but you should always do what you can for yourself first and reading the relevant documentation is always something you can do for yourself.
-
Jul 5th, 2021, 05:10 AM
#3
Re: Read XML file and insert into ListBox
give this a try
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim xelement As XElement = xelement.Load("D:\Testfolder\Ange1992.xml")
Dim xWines As IEnumerable(Of XElement) = xelement.Elements()
' Read the XML
For Each xWine In xWines
'get the values you want
Debug.WriteLine(xWine.@Name & " - " & xWine.@Country & " - " & xWine.@Region)
Next xWine
End Sub
the debug output...
Code:
Canapi Grillo - Italie - Sicilie
Miopasso Fiano - Italie - Sicilie
Elegance Sauvignon Blanc - Frankrijk - Pays d'Oc
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Jul 5th, 2021, 06:04 AM
#4
Thread Starter
New Member
Re: Read XML file and insert into ListBox
Thanks for your comments!
I understand it was a stupid question, but I'm fairly new to visual basic 
I will use the help function more often.
Anyway, the solution of ChrisE worked!
Thank you both!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|