1

私は、 IsolatedStorageからデータを保存して読み取るWindowsPhone 8アプリに取り組んでいます。

Visual Basic .NET と LINQ to XML を使用しています。

データのロード/保存には問題はありませんが、XElement を[***XElementListName* ].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 のドキュメントを参照して検索し、理解しようとしましたが、使用可能な例がありません。また、これを確認しました: 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 に答える