-1

Since I don't want to use a database to save this information I'm trying to save user favorites into a Xml file and load them later.

Currently I'm using this code but this overwrites the previous Xml file completely instead of adding to it:

//Creates new filestream with create and write permissions
FileStream fs = new FileStream("Favo.Xml", FileMode.Create, FileAccess.Write);
//Calls SavoFace.cs
SaveFavo sf = new SaveFavo();
//Fills public string Name with tbname.text
sf.Name = tbName.Text;

lvFavo.Items.Add(tbName.Text);
//Adds name to the list
ls.Add(sf);

//Serializes the filestream and the list
xs.Serialize(fs, ls);
//Closes the file
fs.Close();

(This code came from a youtube video as I have never worked with Xml files before, however I can't seem to find an answer to this specific question.)

How would I go about adding to the Xml file instead of overwriting it completely?

Thank you in advance for the answer.

Eduard Malakhov
  • 1,095
  • 4
  • 13
  • 25
  • 1
    Check this https://stackoverflow.com/questions/20922835/appending-an-existing-xml-file-with-xmlwriter – PSK Feb 04 '18 at 12:16
  • 3
    You are explicitly using `FileMode.Create`, did you even read the description? – Camilo Terevinto Feb 04 '18 at 12:16
  • You solution with have multiple xml identification lines which will not read. Appending you have to remove the id line and then you will have multiple elements at root level which is a not well formed xml. So when reading you will need to set the xml reader to Fragments. – jdweng Feb 04 '18 at 12:43

1 Answers1

0

here is a working sample based on your code

    class Program
{

    static void Main(string[] args)
    {
        List<SaveFavo> ls = new List<SaveFavo>();
        bool bExists = File.Exists("Favo.Xml");
        XmlSerializer xs = new XmlSerializer(typeof(List<SaveFavo>));
        if (!bExists)
        {
            //creates file if file doesn't exist
            using (FileStream fs = File.Create("Favo.Xml"))
            {

                AddFavo(ls, "Test1");
                xs.Serialize(fs, ls);

                //Closes the file
                fs.Close();

            }
        }
        else
        {

            var fs = File.Open("Favo.Xml", FileMode.OpenOrCreate);

            //reads existing file and deserialize into list<SaveFavo>
            ls = xs.Deserialize(fs) as List<SaveFavo>;
            fs.Close();
        }
        //test sample 
        for (int i = 2; i < 10; i++)
            using (FileStream fs = File.OpenWrite("Favo.Xml"))
            //add new content and saves file
            {
                AddFavo(ls, $"Test{i}");
                xs.Serialize(fs, ls);
                fs.Close();
            }

    }

    private static void AddFavo(List<SaveFavo> ls, string favo)
    {
        SaveFavo sf = new SaveFavo();
        //Fills public string Name with tbname.text
        sf.Name = favo;

        //Adds name to the list
        ls.Add(sf);
    }
}

public class SaveFavo
{
    public string Name { get; set; }
    public SaveFavo() { }
}

results

Xavave
  • 645
  • 11
  • 15