1

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

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

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

它会抛出 System.AccessViolationException,但前提是它应该查找并替换绑定到LongListSelector的第一个项目的数据,也是 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 文档来弄清楚它,但它缺乏可用的示例。还检查了这个:如何从字符串中更新/替换 XElement 的元素?

那么,有没有人坐在我可以用来解决这个问题的任何知识上?如果您想看一些代码,请告诉我,虽然它很丑陋。

编辑:省略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
4

1 回答 1

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
于 2013-06-29T11:06:21.137 回答