I’m just starting out on one of my first programming projects in visual studio 2010.
I have an XML file that I have added into the program as an item. It has the following structure:
<lexeme><grapheme>Aalesund</grapheme> <phoneme>'A:lI2s,Vnd</phoneme></lexeme>
There are 400,000 entries like that and what I would like to do is on a button press have a random lexeme selected from the xml file and then two labels populated with the corresponding grapheme and phoneme.
Could anyone point me in the right direction to begin with please? The tutorials I have found are for loading specific amounts of data, not just one random line and reference an external xml file location, not one that is internal to the project.
Thanks in advance.
Edit: I should say that I mean pseudo-random number. I was hoping to find a rand() function, but can’t seem to?
There isn’t a direct way of getting a random line from an XML. And relying on line number is dangerous because if the format of the XML were to be changed from:
to:
Your randomly generated line number might not line up with a
<lexeme>element anymore.I think the best way to do it is to get all of the
<lexeme>elements in a list and randomly generate a number that is within the range of the list:(forgive me, but the code sample is in c#, I don’t trust my VB.Net enough to write my code in VB)
randomLexmewill then have a pseudo-random<lexeme>element and you can parse it as you need to get the appropriate<grapheme>and<phoneme>elements.If you do this though, keep in mind that the .net
Randomclass is pseudo-random and uses the current timestamp as a seed. If you are going to be accessing objects regularly, it would be advisable to make thevar random = new Random()variable as class-level field and create it once and just used theNext()method to get the next number, rather than creating a newRandom()object whenever you need a random number.Here’s a more self contained function in VB.Net: