Question

I'm working on an WindowsPhone 8 app that stores and reads it's data from its IsolatedStorage.

I'm using Visual Basic.NET and LINQ to XML.

There's no problem with loading/saving the data, but I've got a serious issue with replacing an XElement with [***XElementListName*].FirstOrDefault().ReplaceWith([NewXElementName])**.

It throws an System.AccessViolationException, but only if it's supposed to find and replace the data bound to the FIRST item in a LongListSelector, also the first of that element in the XML file. Editing any other item works fine thou. Here's the code from the routine that is supposed to replace the data.

Dim Accounts = From el In XMLData.<accounts>.<account> Where _
                   el.Attribute("title").Value = oiTitle And _
                   el.Attribute("uname").Value = oiName And _
                   el.Attribute("pass").Value = oiPass And _
                   el.Attribute("site").Value = oiSite And _
                   el.Attribute("description").Value = oiDesc _
                   Select el

    Dim acc As XElement = Accounts.FirstOrDefault()

    If acc.Equals(Nothing) Then
        MessageBox.Show("Error while saving edited account. Account not found.", "Account not found", MessageBoxButton.OK)
        Exit Sub
    End If
    Dim tmpstr As String = acc.Attribute("title").Value + _
                           acc.Attribute("uname").Value + _
                           acc.Attribute("pass").Value + _
                           acc.Attribute("site").Value + _
                           acc.Attribute("description").Value

    'Does this during debug to confirm that the replace is performed on the correct item.
    MessageBox.Show(tmpstr, "Info about first item", MessageBoxButton.OK)

    acc.Attribute("title").SetValue(NewInfo.Title)
    acc.Attribute("uname").SetValue(NewInfo.Username)
    acc.Attribute("pass").SetValue(NewInfo.Password)
    acc.Attribute("site").SetValue(NewInfo.Site)
    acc.Attribute("description").SetValue(NewInfo.Description)

    ' This code throws the exception when performing on the first item from the LongListSelector
    Accounts.FirstOrDefault().ReplaceWith(acc)

I've searched and tried to figure it out by looking at the LINQ to XML documentation, but it lacks usable examples. Also checked this: How can I update/replace an element of an XElement from a string?

So, does anyone sit on any knowledge I could use to solve this? If there's some code you'd like to see, just tell me, it's pretty ugly though.

EDIT: Omitted the Accounts.FirstOrDefault().ReplaceWith(acc) line, and it works just fine. Saves everything as it should. Also rewrote some code, here's the new one, and all the related code in that sub.

Public Sub EditAccount(ByVal OldInfo As AccountViewModel, ByVal NewInfo As AccountViewModel)
    Dim IsStore As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    Dim File As New IsolatedStorageFileStream("Data.xml", FileMode.Open, IsStore)

    Dim XMLData As XElement = XElement.Load(File)

    Dim oiTitle As String = OldInfo.Title
    Dim oiName As String = OldInfo.Username
    Dim oiPass As String = OldInfo.Password
    Dim oiSite As String = OldInfo.Site
    Dim oiDesc As String = OldInfo.Description
    Try
        Dim Accounts = From e In XMLData.<accounts>.<account> Where ( _
                         e.Attribute("title").Value = oiTitle And _
                         e.Attribute("uname").Value = oiName And _
                         e.Attribute("pass").Value = oiPass And _
                         e.Attribute("site").Value = oiSite And _
                         e.Attribute("description").Value = oiDesc)
                     Select e Take 1

        Dim Account As XElement = Accounts.FirstOrDefault()

        If Account.Equals(Nothing) Then
            MessageBox.Show("Error while saving edited account. Account not found.", "Account not found", MessageBoxButton.OK)
            Exit Sub
        End If
        Dim tmpstr As String = Account.Attribute("title").Value + _
                               Account.Attribute("uname").Value + _
                               Account.Attribute("pass").Value + _
                               Account.Attribute("site").Value + _
                               Account.Attribute("description").Value

        'MessageBox.Show(tmpstr, "Info about first item", MessageBoxButton.OK)

        Account.Attribute("title").SetValue(NewInfo.Title)
        Account.Attribute("uname").SetValue(NewInfo.Username)
        Account.Attribute("pass").SetValue(NewInfo.Password)
        Account.Attribute("site").SetValue(NewInfo.Site)
        Account.Attribute("description").SetValue(NewInfo.Description)

        File.Close()
        IsStore.DeleteFile("Data.xml")
        File = New IsolatedStorageFileStream("Data.xml", FileMode.Create, IsStore)
        XMLData.Save(File)
        File.Close()
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try
    Me.LoadData()
End Sub
Was it helpful?

Solution

Omitted the Accounts.FirstOrDefault().ReplaceWith(acc) line, and it works just fine. Saves everything as it should. Also rewrote some code, here's the new one, and all the related code in that sub.

Public Sub EditAccount(ByVal OldInfo As AccountViewModel, ByVal NewInfo As AccountViewModel)
    Dim IsStore As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    Dim File As New IsolatedStorageFileStream("Data.xml", FileMode.Open, IsStore)

    Dim XMLData As XElement = XElement.Load(File)

    Dim oiTitle As String = OldInfo.Title
    Dim oiName As String = OldInfo.Username
    Dim oiPass As String = OldInfo.Password
    Dim oiSite As String = OldInfo.Site
    Dim oiDesc As String = OldInfo.Description
    Try
        Dim Accounts = From e In XMLData.<accounts>.<account> Where ( _
                         e.Attribute("title").Value = oiTitle And _
                         e.Attribute("uname").Value = oiName And _
                         e.Attribute("pass").Value = oiPass And _
                         e.Attribute("site").Value = oiSite And _
                         e.Attribute("description").Value = oiDesc)
                     Select e Take 1

        Dim Account As XElement = Accounts.FirstOrDefault()

        If Account.Equals(Nothing) Then
            MessageBox.Show("Error while saving edited account. Account not found.", "Account not found", MessageBoxButton.OK)
            Exit Sub
        End If
        Dim tmpstr As String = Account.Attribute("title").Value + _
                               Account.Attribute("uname").Value + _
                               Account.Attribute("pass").Value + _
                               Account.Attribute("site").Value + _
                               Account.Attribute("description").Value

        'MessageBox.Show(tmpstr, "Info about first item", MessageBoxButton.OK)

        Account.Attribute("title").SetValue(NewInfo.Title)
        Account.Attribute("uname").SetValue(NewInfo.Username)
        Account.Attribute("pass").SetValue(NewInfo.Password)
        Account.Attribute("site").SetValue(NewInfo.Site)
        Account.Attribute("description").SetValue(NewInfo.Description)

        File.Close()
        IsStore.DeleteFile("Data.xml")
        File = New IsolatedStorageFileStream("Data.xml", FileMode.Create, IsStore)
        XMLData.Save(File)
        File.Close()
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try
    Me.LoadData()
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top