gpt4 book ai didi

c# - XMLWriter 添加到现有文件

转载 作者:太空宇宙 更新时间:2023-11-03 15:27:01 28 4
gpt4 key购买 nike

下面是代码。我的问题是,如果我想创建此文件,然后稍后使用 XmlWriter 重新打开它并向当前文件添加更多内容,该怎么做?喜欢使用 XmlWriter

进入并编辑此 xml
using System.Xml;

class Program
{
class Employee
{
int _id;
string _firstName;
string _lastName;
int _salary;

public Employee(int id, string firstName, string lastName, int salary)
{
this._id = id;
this._firstName = firstName;
this._lastName = lastName;
this._salary = salary;
}

public int Id { get { return _id; } }
public string FirstName { get { return _firstName; } }
public string LastName { get { return _lastName; } }
public int Salary { get { return _salary; } }
}

static void Main()
{
Employee[] employees = new Employee[4];
employees[0] = new Employee(1, "David", "Smith", 10000);
employees[1] = new Employee(3, "Mark", "Drinkwater", 30000);
employees[2] = new Employee(4, "Norah", "Miller", 20000);
employees[3] = 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();
}
}
}

最佳答案

我将重构 Employee 类并创建一个 Employees 类用于序列化,如下所示:

[XmlRoot("Employees")]
public class Employees : List<Employee> { }

public class Employee
{
int _id;
string _firstName;
string _lastName;
int _salary;

public Employee() { }

public Employee(int id, string firstName, string lastName, int salary)
{
this._id = id;
this._firstName = firstName;
this._lastName = lastName;
this._salary = salary;
}

[XmlElement("ID")]
public int Id
{
get { return _id; }
set { _id = value; }
}
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public int Salary
{
get { return _salary; }
set { _salary = value; }
}
}

现在您可以轻松地做您想做的事:

Employees employees = new Employees();
employees.Add(new Employee(1, "David", "Smith", 10000));
employees.Add(new Employee(3, "Mark", "Drinkwater", 30000));
employees.Add(new Employee(4, "Norah", "Miller", 20000));
employees.Add(new Employee(12, "Cecil", "Walker", 120000));

XmlSerializer ser = new XmlSerializer(typeof(Employees));


ser.Serialize(Console.OpenStandardOutput(), employees);

Console.ReadKey();

为了找回它

XmlSerializer ser = new XmlSerializer(typeof(Employees));
Employees emps = (Employees)ser.Deserialize(File.OpenRead("employees.xml"));
emps.Add(new Employeee...);
ser.Serialize(File.OpenWrite("employees.xml"), emps);

随着对象的增长,只用新属性修饰成员比重写涉及的 XmlReader/XmlWriter 代码要容易得多。

关于c# - XMLWriter 添加到现有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34876167/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com