xml Append,File.Add ...覆盖原始数据

时间:2017-11-18 20:29:39

标签: xml linq append

我遇到了xml.linq的问题。 我能够创建一个文件并添加一些细节,但是当我尝试添加更多信息时,它会覆盖原始数据。

Customer newCustomer = new Customer();
Console.WriteLine("Please Enter new Customer Id");
int id = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please Enter The Customers First Name:");
string firstName = Console.ReadLine();
Console.WriteLine("Please Enter The Customers Surname:");
string lastName = Console.ReadLine();
Console.WriteLine("Please Enter a Contact Telephone Number:");
string phoneNumber = Console.ReadLine();
Console.WriteLine("Please Enter The First Line of the Delivery Address:");
string firstLineAddress = Console.ReadLine();
Console.WriteLine("Please Enter Second Line of the Address:");
string secondLineAddress = Console.ReadLine();
Console.WriteLine("Please Enter Town / City:");
string townCity = Console.ReadLine();
Console.WriteLine("Please Enter Postcode");
string postCode = Console.ReadLine();
Console.WriteLine("Please Enter Customers Email Address");
string emailAddress = Console.ReadLine();
XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Customer Database"),
new XElement("customer", new XAttribute("CustomerId", id),
                          new XElement("firstName", firstName),
                          new XElement("lastName", lastName),
                          new XElement("firstLineAddress", firstLineAddress),
                          new XElement("secondLineAddress", secondLineAddress),
                          new XElement("cityTown", townCity),
                          new XElement("postCode", postCode),
                          new XElement("phoneNumber", phoneNumber),
                          new XElement("emailAddress", emailAddress)));
doc.Save("CustomerDatabase\\Customer.xml");

我明白为什么会发生这种情况,我无法弄清楚如何归档。附近(或类似的东西。)任何人都能指出我正确的方向吗?
感谢

1 个答案:

答案 0 :(得分:0)

您是否希望在同一个文件中使用单独的独立Xml文档:

<?xml version="1.0" encoding="utf-8"?>
<customer>...</customer>
<?xml version="1.0" encoding="utf-8"?>
<customer>...</customer>

然后移除doc.Save行并将其替换为File.AppendAllText("CustomerDatabase\\Customer.xml", doc.ToString());

或者包含许多客户的单个文档,例如:

<?xml version="1.0" encoding="utf-8"?>
<customers>
  <customer>...</customer>
  <customer>...</customer>
</customers>

然后,您需要从磁盘加载原始数据,将新元素添加到数据中,然后将整个内容保存回磁盘。

var newCustomer = new XElement("customer", new XAttribute("CustomerId", id),
                                  new XElement("firstName", firstName),
                                  new XElement("lastName", lastName),
                                  new XElement("firstLineAddress", firstLineAddress),
                                  new XElement("secondLineAddress", secondLineAddress),
                                  new XElement("cityTown", townCity),
                                  new XElement("postCode", postCode),
                                  new XElement("phoneNumber", phoneNumber),
                                  new XElement("emailAddress", emailAddress)));

XDocument doc = XDocument.Load("CustomerDatabase\\Customer.xml");
var customers= doc.Element("customers");
customers.Add(newCustomer);
doc.Save("CustomerDatabase\\Customer.xml");

这两种方法都有利有弊: 第一个很可能不是你想要的,这通常不是你存储xml数据的方式。第二个更好,但是当文档变大时磁盘很重。在这种情况下,首选选项是将单个XDocument存储在内存中并添加到内存中,然后保存到磁盘而不是每次都进行读写。