user1050667
user1050667

Reputation: 453

Appending data to existing xml file

I don't want to overwrite the data in XML File i want to save the old and new data in XML file.

I have a three classes:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }       

    public Person()
    {
    }

    public override string ToString()
    {
        return FirstName + " " +LastName + "\t" +Email;
    }
}

public class Student : Person
{
    public Student()
    {
    }
    public double AssessmentGrade { get; set; }
    public double AssignmentGrade { get; set; }

    public override string ToString()
    {
        return base.ToString() + "," +AssessmentGrade + "," + AssignmentGrade;
    }
}

public class Teacher : Person
{
    public int RoomNumber { get; set; }

    public override string ToString()
    {
        return base.ToString() + "," + RoomNumber;
    }
}

I have One more class where i just call the data from PeronDB (Class)

public class Persons
{
    private List<Person> persons = null;

      public void Save()
    {
        PersonDB.SavePersons(persons);
    }
 }

InPersonDB class I am doing save the whole data in XML File but it overwrite

public class PersonDB
{
    private const string path = @"..\..\Persons.xml";


    public static void SavePersons(List<Person> Persons)
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.IndentChars = ("   ");

        XmlWriter xmlOut = XmlWriter.Create(path, settings);

        xmlOut.WriteStartDocument();
        xmlOut.WriteStartElement("Persons");

        foreach (Person person in Persons)
        {

                if (typeof(Student).IsInstanceOfType(person))
                {
                    AppendStudentInfo(person, xmlOut);
                }
                else if (typeof(Teacher).IsInstanceOfType(person))
                {
                    AppendTeacherInfo(person, xmlOut);
                }
                else
                {
                    AppendPersonInfo(person, xmlOut);
                }
        }

        xmlOut.WriteEndElement();
        xmlOut.Close();
    }

And in the Form_Load event I am doing:

    Persons personList = null;

    private void FillPersonlstBox()
    {
        Person p;
        listBox1.Items.Clear();
        for (int i = 0; i < personList.Count; i++)
        {
            p = personList[i];
            listBox1.Items.Add(p.ToString());
        }
    }

    private void Save_Click(object sender, EventArgs e)
    {
        HandleChange(personList);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        personList.Changed += new Persons.ChangeHandler(HandleChange);
    }

    private void HandleChange(Persons Persons)
    {
        Persons.Save();
        FillPersonlstBox();
    }

    private void Load_Click(object sender, EventArgs e)
    {
        personList.Changed += new Persons.ChangeHandler(HandleChange);
        personList.Fill();
        FillPersonlstBox();
    }

When I click the save button, it overwrites the data it delete the old data and saves the new data, Even I press the first Load button and load the data and then add more data after that I save the press button it still overwrite..

Upvotes: 1

Views: 503

Answers (1)

Trevor Tubbs
Trevor Tubbs

Reputation: 2117

You have the file path in a constant in PersonDB. Instead pass make the file path a parameter to Save() and SavePersons(). That way you can prompt the user for a file name and pass it in or have a method generate a name based on a convention.

EDIT:

Here is an example of changing the signature of SavePersons to include the path as a parameter.

public static void SavePersons(List<Person> Persons, String path)
{
//your code
}

Upvotes: 1

Related Questions