将新数据添加到现有xml文件中

时间:2013-11-07 08:21:16

标签: c# xml

我正在从员工类中编写一个xml文件。现在我想将员工的新记录添加到现有的xml文件中。我怎么能这样做?

现在点击按钮我想在XML中添加员工的新记录。

----- Xml文件----

<?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>

-----背后的代码-----

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

1 个答案:

答案 0 :(得分:2)

你去了:

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