c# - XMLWriter 添加到现有文件

标签 c# xml

下面是代码。我的问题是,如果我想创建此文件,然后稍后使用 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/

相关文章:

c# - 如何在 C# Windows 窗体对话框中显示标准错误图标?

类似于ZPT(属性语言)的Java模板库

javascript - ajax xml 加载后 jQuery 切换不起作用

android - 如何强制 SharedPreferences 保存?

c# - 内存中集合的大小与磁盘上集合的大小

c# - Xamarin Async ViewDidAppear 在 ViewDidLoad 期间调用

PHP 解析 XML 时出错(simpleXML)

c# - XML 中缺少标记会阻止 XmlSerializer 创建属性

c# - CS1607 : Assembly generation -- The version '1.4.0.85725' specified for the 'file version' is not in the normal 'major.minor.build.revision' format

C#泛型重构