Not Able to Modify XML Value in XML file VB RRS feed

  • 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" ?>

    Here is the vb code.

    Dim scoredb As XDocument = XDocument.Load("score.xml")

    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!"
                        Dim sb As StringBuilder = New StringBuilder()
                        Dim tr As TextWriter = New StringWriter(sb)
    <?xml version="1.0" encoding="utf-8" ?>

    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?



    Saturday, June 7, 2014 11:07 PM


  • 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
                        lblGameOver.Text = "Great Job!"
                        Dim compareValue As Integer
                            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.



    • Edited by David.Warwick Sunday, June 8, 2014 3:58 PM
    • Marked as answer by Anne Jing Monday, June 9, 2014 1:13 AM
    Sunday, June 8, 2014 3:39 PM