Search code examples
c#xmllinqlinq-to-xmlxelement

Bug: List remains in memory if file deleted, and saves data twice if file is deleted


got an odd bug in my code and I can't see why it's behaving the way it is. I'm not the most experienced code warrior so hoping someone with a bit more experience can see why.

I'm saving a list of objects to an XML file. I pass the list of the objects to my function for saving and I have code in place to check if the object is already there and only save the object to the XML file if it doesn't exist. If I press the Save button once, it creates the file fine, and saves as expected. If I press save again it again works as expected and only new objects are saved.

The bug I have is this...

If I press the save button, say, 3 times and then delete the file, when I press save and recreate the file the list is saved 3 times. As if the previous lists are still floating around and just getting added on top of each other.

Here's my code for saving... In case it helps, my code HasElement() is an extension method for XElement, and simply returns a FirstOrDefault(). Save only happens when this is returned as null.

  public void SaveDB(List<ContactList> cl)
         {
             if (cl != null)
             {
                 if (!File.Exists(DBPath))
                 {
                     XDocument doc = new XDocument(
                         new XDeclaration("1.0", "utf-8", "yes"),
                         new XElement("Contacts")
                         );
                     doc.Save(DBPath);
                     MessageBox.Show("File Created: " + DBPath);
                 }
                 MessageBox.Show(DBPath + " already exists!");
                 XDocument Doc = XDocument.Load(DBPath);
                 List<XElement> elmAdd = new List<XElement>();
                 XElement root = Doc.Element("Contacts");

                 foreach (ContactList CL in cl)
                 { 
                    if (root.HasElement(CL.Name) == null)
                    {
                     if (CL.Selected == true)
                         {
                             XElement eName = new XElement(CL.Name, "true");
                             elmAdd.Add(eName);
                         }
                         else if (CL.Selected == false)
                         {
                             XElement eName = new XElement(CL.Name, "false");
                             elmAdd.Add(eName);
                         }
                     }
                 }


                 MessageBox.Show("Lists saved");
                 Doc.Element("Contacts").Add(elmAdd);
                 Doc.Save(DBPath);
             } // End if null
             else
             {
                 MessageBox.Show("Debug: List is empty");
             }
         } // end method

Solution

  • Most probably the bug is out of scope of this function and somehow List with duplicate entries has been passed. You can handle such situation within yours function by Grouping using Distinct or changing yours code to append child elements one by one. But more appropriate solution will be to determine how duplicates were added to source list.