如何在c#中将数据添加到现有的xml文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19894626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:04:35  来源:igfitidea点击:

How to add data to an existing xml file in c#

c#xml

提问by T-D

I'm using this c# code to write data to xml file:

我正在使用此 c# 代码将数据写入 xml 文件:

Employee[] employees = new Employee[2];
employees[0] = new Employee(1, "David", "Smith", 10000);
employees[1] = 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();
}

Now suppose I restart my application and I want to add new data to the xml file without losing the existed data, using the same way will overwrite the data on my xml file, I tried to figure out how to do that and I searched for a similar example but I couldn't come to anything , any ideas ??

现在假设我重新启动我的应用程序,我想在不丢失现有数据的情况下向 xml 文件添加新数据,使用相同的方式将覆盖我的 xml 文件上的数据,我试图弄清楚如何做到这一点,我搜索了一个类似的例子,但我什么也想不出来,有什么想法吗??

采纳答案by Ceelie

Perhaps you should look at some examples using datasets and xml:

也许您应该查看一些使用数据集和 xml 的示例:

http://www.codeproject.com/Articles/13854/Using-XML-as-Database-with-Dataset

http://www.codeproject.com/Articles/13854/Using-XML-as-Database-with-Dataset

or use System.Xml.Serialization.XmlSerializer, when you dont't have amount of records.

或使用 System.Xml.Serialization.XmlSerializer,当您没有大量记录时。

Example using XmlDocument

使用 XmlDocument 的示例

XmlDocument xd = new XmlDocument();
xd.Load("employees.xml");
XmlNode nl = xd.SelectSingleNode("//Employees");
XmlDocument xd2 = new XmlDocument();
xd2.LoadXml("<Employee><ID>20</ID><FirstName>Clair</FirstName><LastName>Doner</LastName><Salary>13000</Salary></Employee>");
XmlNode n = xd.ImportNode(xd2.FirstChild,true);
nl.AppendChild(n);
xd.Save(Console.Out);

回答by Anders Abel

Using an xml writer for small amounts of data is awkward. You would be better of using an XDocumentthat you either initialize from scratch for the first run, or read from an existing file in subsequent runs.

对少量数据使用 xml 编写器很尴尬。您最好使用XDocument您在第一次运行时从头开始初始化,或者在后续运行中从现有文件中读取的 。

Using XDocumentyou can manipulate the XML with XElementand XAttributeinstances and then write the entire thing out to a file when you want to persist it.

使用XDocument您可以使用XElementXAttribute实例操作 XML,然后在您想要保留它时将整个内容写入文件。