2

Below is the code. My question is, if i wanted to create this file then later use XmlWriter to reopen it and add more stuff to a current file how would one do that? like to go in and edit this xml using XmlWriter

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();
        }
    }
}
ardila
  • 1,277
  • 1
  • 13
  • 24
georgieboy
  • 155
  • 1
  • 10
  • 4
    Your best bet would to be to read the file in, deserialize it to employees, make the changes to the objects, then overwrite the file. Which begs the question - why aren't you using the native XML serialization? – stuartd Jan 19 '16 at 11:56
  • 1
    Like @stuartd said: Deserialize - change your objects - Serialize. Since you use default types like int and string you might have a look at the XmlSerializer in order to (de)serialize your 'Employee'. So you don't need to do the (de)serialization by yourself. This may help you to get in: http://stackoverflow.com/questions/12924221/using-xmlserializer-with-an-array-in-the-root-element – myst3rium Jan 19 '16 at 12:00
  • 1
    Possible duplicate of [Appending an existing XML file with XmlWriter](http://stackoverflow.com/questions/20922835/appending-an-existing-xml-file-with-xmlwriter) – Volkan Paksoy Jan 19 '16 at 13:56

1 Answers1

0

I would refactor the Employee class and create an Employees class for serialization like so:

[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; }
    }
}

And now you can easily do what you want:

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();

And to get it back

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);

And as your object grows, it's much easier to just decorate members with new attributes rather than rewriting involved XmlReader/XmlWriter code.

Dan Field
  • 20,885
  • 5
  • 55
  • 71