Retrieving high scores from an Xml file

NKN

Member
Joined
Dec 9, 2011
Messages
19
Programming Experience
Beginner
This is the Xml file where the high scores are saved:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
<User Name="Player1">
<HighScore>626</HighScore>
</User>
<User Name="Player2">
<HighScore>352</HighScore>
</User>
</Root>

And this is the code to retrieve the highscores:

Dim m_xmlr As XmlTextReader


m_xmlr = New XmlTextReader("C:\Users\Nicolas\Desktop\Scores.xml")
m_xmlr.WhiteSpaceHandling = WhiteSpaceHandling.NONE
m_xmlr.Read()
m_xmlr.Read()


While Not m_xmlr.EOF


m_xmlr.Read()


If Not m_xmlr.IsStartElement() Then
Exit While
End If


Dim Username = m_xmlr.GetAttribute("Name")
m_xmlr.Read()
Dim HighScore = m_xmlr.ReadElementString("HighScore")






End While
m_xmlr.Close()

But this code only retrieves the last high score how can i get all the players with their high score? for example have two list boxes one for the players name and the second for the high scores and for each player in the Xml file the player's name adds to the first list box and his high score gets added to the second.
 
Hi,

For ease, why not read the data into a DataSet and then just set the DataSource of a DataGridView to the DataSet table?

Try this example:-

VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  Dim myDS As New DataSet
  myDS.ReadXml("d:\temp\YourXMLFileName.xml")
  DataGridView1.DataSource = myDS.Tables(0)
 
  For Each DR As DataGridViewRow In DataGridView1.Rows
    If Not DR.IsNewRow Then
      MsgBox("Name = " & DR.Cells(1).Value.ToString & " - " & "Score = " & DR.Cells(0).Value.ToString)
    End If
  Next
End Sub

Hope that helps.

Cheers,

Ian
 
Back
Top