XElement.ReplaceWith抛出System.AccessViolationException

时间:2013-06-28 17:48:32

标签: vb.net windows-phone-8 linq-to-xml access-violation xelement

我正在开发一个 WindowsPhone 8 应用程序,用于存储和读取其 IsolatedStorage 中的数据。

我正在使用Visual Basic.NET和LINQ to XML。

加载/保存数据没有问题,但是我用 [*** XElementListName * ]替换XElement时遇到了严重问题.FirstOrDefault()。ReplaceWith([ NewXElementName ])**。

它抛出一个System.AccessViolationException,但只有当它应该找到并替换绑定到 LongListSelector FIRST 项的数据时,它才是该元素的第一个在XML文件中。编辑任何其他项目你工作得很好。 这是应该替换数据的例程中的代码。

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)

我已经搜索过并试图通过查看LINQ to XML文档来解决这个问题,但它缺少可用的示例。还检查了这个:How can I update/replace an element of an XElement from a string?

那么,有没有人会依赖我可以用来解决这个问题的任何知识? 如果您想看到一些代码,请告诉我,但它非常难看。

编辑:省略了 Accounts.FirstOrDefault()。ReplaceWith(acc)行,它运行正常。保存应有的一切。还重写了一些代码,这里是新代码,以及该子代码中的所有相关代码。

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

1 个答案:

答案 0 :(得分:0)

忽略 Accounts.FirstOrDefault()。ReplaceWith(acc)行,它运行正常。保存应有的一切。还重写了一些代码,这里是新代码,以及该子代码中的所有相关代码。

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