'Remove record in XML file using lists in vb.net
I am trying to learn how to use XML file for form data storage, and I am having problems when trying to remove an existing record before rewriting it (modified or not).
Private Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click
Dim Data As New List(Of Record)
If System.IO.File.Exists(XMLFileName) Then
Using xmlString = New StreamReader(XMLFileName)
Data = DeserializeFromXml(Of List(Of Record))(xmlString.ReadToEnd().ToString())
End Using
End If
Dim results = Data.Where(Function(x) x.ProductID = ProductID.Text).FirstOrDefault() 'searches for ProductID
Data.Remove(results) 'removes existing record as it will be rewritten
' next section rewrites the record (modified or not)
FindControls(Me, Data)
Dim xml As New XmlSerializer(Data.GetType)
Using writer As New FileStream(XMLFileName, FileMode.Create)
xml.Serialize(writer, Data)
End Using
End Sub
It appears that the statement "Data.Remove(results)" does not work, because if I comment out the last section that rewrites the record, no record is removed from the file. The code and the xml file and a pic of the form are here:https://www.dropbox.com/s/gyewm2ml06h6b44/Learning.zip?dl=0
Could anyone explain to me what am I doing wrong? Thank you
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
