我正在使用此 c# 代码将数据写入 xml 文件:
Employee[] employees = new Employee[2];
employees[0] = new Employee(1, "David", "Smith", 10000);
employees[1] = new Employee(12, "Cecil", "Walker", 120000);
using (XmlWriter writer = XmlWriter.Create("employees.xml"))
{
writer.WriteStartDocument();
writer.WriteStartElement("Employees");
foreach (Employee employee in employees)
{
writer.WriteStartElement("Employee");
writer.WriteElementString("ID", employee.Id.ToString());
writer.WriteElementString("FirstName", employee.FirstName);
writer.WriteElementString("LastName", employee.LastName);
writer.WriteElementString("Salary", employee.Salary.ToString());
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
}
现在假设我重新启动我的应用程序,并且我想在不丢失现有数据的情况下将新数据添加到 xml 文件中,使用相同的方式将覆盖我的 xml 文件上的数据,我试图弄清楚如何做到这一点,然后我搜索了一个类似的例子,但我什么都想不出来,有什么想法吗?