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

C# 将新数据添加到现有xml文件中,c#,xml,C#,Xml,我正在从employee类编写一个xml文件。现在,我想将新的员工记录添加到现有的xml文件中。我该怎么做 现在单击一个按钮,我想在XML中添加一个新的雇员记录 -----Xml文件---- 好了: XDocument xdoc = XDocument.Load(path_to_xml); xdoc.Element("metadata").Add( new XElement("Employee", n

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

现在单击一个按钮,我想在XML中添加一个新的雇员记录

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

好了:

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);
好了:

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);
好了:

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);
好了:

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

与使用
XmlWriter
相比,使用LINQ-to-XML要简单得多。从标题中删除cSharp(不需要,我们有c#Tag!)并添加XML标记以增加曝光率。可以使用LINQ-to-XML,也可以阅读XML序列化。实际上,使用它将XML映射到对象非常容易,所以您可以创建新员工,添加到根对象并序列化为XML。与使用
XmlWriter
,相比,使用LINQ到XML要简单得多。从标题中删除cSharp(无需,我们有c#Tag!)并添加了Xml标记以增加曝光率。可以使用LINQ转换Xml,也可以阅读Xml序列化。实际上,使用它将XML映射到对象非常容易,所以您可以创建新员工,添加到根对象并序列化为XML。与使用
XmlWriter
,相比,使用LINQ到XML要简单得多。从标题中删除cSharp(无需,我们有c#Tag!)并添加了Xml标记以增加曝光率。可以使用LINQ转换Xml,也可以阅读Xml序列化。实际上,使用它将XML映射到对象非常容易,所以您可以创建新员工,添加到根对象并序列化为XML。与使用
XmlWriter
,相比,使用LINQ到XML要简单得多。从标题中删除cSharp(无需,我们有c#Tag!)并添加了Xml标记以增加曝光率。可以使用LINQ转换Xml,也可以阅读Xml序列化。实际上,使用它将XML映射到对象非常容易,所以您可以创建新员工,添加到根对象并序列化为XML。