locked
Not Able to Modify XML Value in XML file VB

    Question

  • I want to store a single time value in an XML file. I am able to read from the XML document, but not able to modify the entry. Here is the XML file called score.xml. It is stored in the root folder.

    <?xml version="1.0" encoding="utf-8" ?>
    <HighScore>00:00:00</HighScore>

    Here is the vb code.

    Dim scoredb As XDocument = XDocument.Load("score.xml")
    scoredb.Element("HighScore").SetValue(txtElapsed.Text)

    So, I want to set the value of the txtElapsed textblock to overwrite the 00:00:00 in the xml document.

    Any help would be appreciated.

    Edit. I am getting closer I think, but I am not quite there yet.  I have figured out that after I set the value, I have to save. I am able to set the value now, but it is not saving. If I play the game all the way through and then click to play again, the value is retained, but if I quit the debugger and then start the debugger again, the value is lost and the xml file still has the default time of 00:00:00. My code currently looks like this.    

    Dim ScoreDB As XDocument = XDocument.Load("score.xml")
    lblGameOver.Text = "Great Job!"
                        ScoreDB.Element("Score").Element("HighScore").SetValue(txtElapsed.Text)
                        Dim sb As StringBuilder = New StringBuilder()
                        Dim tr As TextWriter = New StringWriter(sb)
                        ScoreDB.Save(tr)
    <?xml version="1.0" encoding="utf-8" ?>
    <Score>
    <HighScore>00:00:00</HighScore>
    </Score>

    Steping through the code while it's running, tr contains the XML tags and the time value that it should, so I can't figure out why it's not actually writing to the xml file, score.xml.  Can anyone offer any suggestions?

    Thanks,

    David

                                                                              
    Saturday, June 07, 2014 11:07 PM

Answers

  • I solved my problem using a different technique. Since what I really wanted to do is store one piece of App Data, the high score, I created a setting and stored it in the users app data folder. It is very easy to do. Here is the relevant code.

     Dim localSettings As Windows.Storage.ApplicationDataContainer = Windows.Storage.ApplicationData.Current.LocalSettings
        Dim localFolder As Windows.Storage.StorageFolder = Windows.Storage.ApplicationData.Current.LocalFolder
    
    Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
               
    
            If (localSettings.Values("exampleSetting") Is Nothing) Then localSettings.Values("exampleSetting") = txtElapsed.Text
            localSettings.Values("exampleSetting") = "00:05:00" ' Used for Testing so I can beat the time!
        End Sub
    
    Public Sub StartGame()
                    
    
            Dim Settingsvalue As Object = localSettings.Values("exampleSetting")
            txtScore.Text = Settingsvalue.ToString
            
        End Sub
    
    Private Sub Image1_PointerReleased(sender As Object, e As PointerRoutedEventArgs) Handles Image1.PointerReleased, Image2.PointerReleased, Image3.PointerReleased, _
            Image4.PointerReleased, Image5.PointerReleased, Image6.PointerReleased, Image7.PointerReleased, Image8.PointerReleased, Image9.PointerReleased, Image10.PointerReleased, _
            Image11.PointerReleased, Image12.PointerReleased, Image13.PointerReleased, Image14.PointerReleased, Image15.PointerReleased, Image16.PointerReleased, Image17.PointerReleased, _
            Image18.PointerReleased, Image19.PointerReleased, Image20.PointerReleased, Image21.PointerReleased, Image22.PointerReleased, Image23.PointerReleased, Image24.PointerReleased, _
            Image25.PointerReleased, Image26.PointerReleased, Image27.PointerReleased, Image28.PointerReleased, Image29.PointerReleased, Image30.PointerReleased, Image31.PointerReleased, _
            Image32.PointerReleased, Image33.PointerReleased, Image34.PointerReleased, Image35.PointerReleased, Image36.PointerReleased
            If Button1.Visibility = Windows.UI.Xaml.Visibility.Collapsed Then
    
                
                    If intNumTiles = 0 Then
                        TimerTotal.Stop()
                        Applause.Play()
    
                        lblGameOver.Text = "Great Job!"
    
                        Dim compareValue As Integer
                        Try
                            compareValue = DateTime.ParseExact(txtScore.Text, "hh:mm:ss", CultureInfo.InvariantCulture).CompareTo(localSettings.Values("exampleSetting"))
                        Catch exp As ArgumentException
                            'System.Console.WriteLine("Value is not a DateTime")
                        End Try
    
                        If compareValue > 0 Then
                            localSettings.Values("exampleSetting") = txtElapsed.Text
    
                        End If
                        
                        Button1.Visibility = Windows.UI.Xaml.Visibility.Visible
                    End If
                End If
            End If
        End Sub

    Hopefully this will help someone.

    Sincerely,

    David



    Sunday, June 08, 2014 3:39 PM