Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8401143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:43:48+00:00 2026-06-09T21:43:48+00:00

I don’t want to overwrite the data in XML File i want to save

  • 0

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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:43:50+00:00Added an answer on June 9, 2026 at 9:43 pm

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't you hate it when you have class Foobar { public: Something& getSomething(int index)
I have just tried to save a simple *.rtf file with some websites and
don't know better title for this, but here's my code. I have class user
Don't ask me how but I'm in a situation where I have DCPs published
Don't know if I worded the question right, but basically what I want to
Don't they both have to convert to machine code at some point to execute
Don't overthink this - there's a very commonly used term and I ... have
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't think my virtualhost is working correctly. This is what I have inside of
Don't ask why but I have the requirement to draw a border around certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.