0

I am writing a xml file from a employee class. now i want to add new record of employee into existing xml file . how can i do this?

Now on a button click i want to add a new record of employee in XML.

----- Xml file ----

<?xml version="1.0" encoding="utf-8"?>
<metadata>
  <Employee ID="1" firstName="Usman" lastName="Shahzad" Salary="1000" />
  <Employee ID="2" firstName="Mubasshir" lastName="Aashiq" Salary="1001" />
  <Employee ID="3" firstName="Ishitiaq" lastName="Ch." Salary="1002" />
</metadata>

----- Code Behind -----

  private void btnGet_Click(object sender, EventArgs e)
        {
            Employee[] employees = new Employee[3];
            employees[0] = new Employee(1, "Usman", "Shahzad", 1000);
            employees[1] = new Employee(2, "Mubasshir", "Aashiq", 1001);
            employees[2] = new Employee(3, "Ishitiaq", "Ch.", 1002);


            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            string filename = @"Users\text1.xml";
            Directory.CreateDirectory(Path.GetDirectoryName(filename));
            using (XmlWriter xmlWriter = XmlWriter.Create(filename, settings))
            {
                xmlWriter.WriteStartDocument();
                xmlWriter.WriteStartElement("metadata");
                foreach (Employee a in employees)
                {
                    xmlWriter.WriteStartElement("Employee");
                    xmlWriter.WriteAttributeString("ID", Convert.ToString(a.Id));
                    xmlWriter.WriteAttributeString("firstName", Convert.ToString(a.FirstName));
                    xmlWriter.WriteAttributeString("lastName", Convert.ToString(a.LastName));
                    xmlWriter.WriteAttributeString("Salary", Convert.ToString(a.Salary));
                    xmlWriter.WriteEndElement();
                }
                xmlWriter.WriteEndElement();
            }
            DialogResult dr = MessageBox.Show("XML file creation Done\nDo you want to load it into grid view?", "Testing", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (dr == DialogResult.OK)
            {
                ds.ReadXml(@"Users\text1.xml");
                dataGridView1.DataSource = ds.Tables[0];
            }
            else
                return;

        }
        class Employee
        {
            Int32 _id;
            string _firstName;
            string _lastName;
            Int32 _salary;

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

            public Int32 Id { get { return _id; } }
            public string FirstName { get { return _firstName; } }
            public string LastName { get { return _lastName; } }
            public Int32 Salary { get { return _salary; } }
        }
3
  • 1
    Rather than using XmlWriter, you'd be a lot better off using LINQ to XML - it's much simpler. Commented Nov 7, 2013 at 8:22
  • Removed cSharp from title (No need, we have c# Tag!) and added Xml tag to increase exposure. Commented Nov 7, 2013 at 8:24
  • Either use LINQ to XML, or read up on XML serialization. It's actually surprisingly easy to map XML to objects using it, so you can just create new employees, add to a root object and serialize into XML. Commented Nov 7, 2013 at 8:31

1 Answer 1

3

There you go:

XDocument xdoc = XDocument.Load(path_to_xml);
            xdoc.Element("metadata").Add(
                new XElement("Employee",
                    new XAttribute("ID", Convert.ToString(Employee.Id)),
                    new XAttribute("firstName", Convert.ToString(Employee.FirstName)),
                    new XAttribute("lastName", Convert.ToString(Employee.LastName)),
                    new XAttribute("Salary", Convert.ToString(Employee.Salary))
                    ));
            xdoc.Save(path_to_xml);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.