I'm trying to make a xml file using random numbers with name of user' but I get a NullReferenceException was unhandled
I have a start button, stop button, label and a text box and dont know if anything is set right. Please help
I have a start button, stop button, label and a text box and dont know if anything is set right. Please help
VB.NET:
Public Class Form1
'Random to fake a score
Dim rnd As New Random
Dim userName As XElement
Dim newUser As XAttribute
Dim scoresDoc As XDocument
Dim userData As XElement
Dim highScore As Integer = 0
Private Sub btnStartGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartGame.Click
scoresDoc = XDocument.Load("C:\Temp\HighScores.xml")
'Load User
userData = (From node In scoresDoc.Descendants("User")
Where node.Attribute("name").Value = userName
Select node).FirstOrDefault
lblHighScore.Text = "highScore"
'Set High Score if User is returning
If userData IsNot Nothing Then
highScore = CInt(userData.<HighScore>.Value)
lblHighScore.Text = "highScore"
End If
End Sub
Private Sub btnEndGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEndGame.Click
Dim currentScore = rnd.Next(1, 1001)
'Check if User exists
If userData IsNot Nothing Then
If currentScore > highScore Then
'Update HighScore element
userData.SetElementValue("HighScore", currentScore)
MessageBox.Show("New High Score of " & currentScore.ToString() & "!")
highScore = currentScore
lblHighScore.Text = "highScore"
End If
Else
'Create a user
Dim newUser = <User name=<%= userName %>>
<HighScore><%= currentScore %></HighScore>
</User>
scoresDoc.Element("Root").Add(newUser)
End If
scoresDoc.Save("C:\Temp\HighScores.xml")
End Sub
End Class