过度编写现有的XML文件 [英] over writing the existing XML file

查看:73
本文介绍了过度编写现有的XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经有了一个XML文件。我希望在不丢失该文件的情况下使用C#.NET Winforms应用程序来编写该文件。

解决方案

无法直接覆盖某些内容在不丢失现有数据的情况下:正如ThePhantomUpvoter所说,您可能需要添加数据。使用XML意味着读取文件,将XML数据添加到最终为基于表的数据中,并将其写回文件。同样有很多方法可以做到这一点 - 这取决于你的应用程序的其余部分。



另一种可能性是你实际意味着你要保留一个原始文件的副本保持不变并创建新版本。在这种情况下,您需要像以前一样读取文件,然后使用File.Copy方法进行备份,然后再使用添加的数据编写新版本。


在内存中读取文件,然后进行适当的更改(例如用新信息增加它),最后将数据写回文件。

有许多读/修改/写的教程 XML C#的文件,只需使用 Google [ ^ ]。

使用.Net,您不能在不丢失数据的情况下重写(附加)XML文件。因此,最简单的解决方案是读取文件,将其数据附加到内存中,然后再次写入该文件。您将使用旧数据+新数据完全重写。


hi,
I already have a XML file. I want to over write that file without losing earliear data in that file, with a C#.NET Winforms application.

解决方案

There is not way to directly "overwrite" something without losing the existing data: as ThePhantomUpvoter has said, you probably need to Append the data instead. With XML that means reading the file, adding your XML data to the table based data that ends up as, and writing it back to the file. Again there are a number of ways to do that - it depends on the rest of your application.

The other possibility is that you actual mean that you want to keep a copy of the original file unchanged and create a new version. In which case you would want to read the file as before, then use the File.Copy method to make a backup before writing the new version with the added data.


Read the file in memory, then make the appropriate changes (e.g. augment it with new info) and finally write back the data to the file.
There are many tutorial for read/modify/write XML files with C#, just use Google[^].


With .Net, you can not over write (append) an XML file without losing data. So, simplest possible solution is to read the file, append its data in memory, and then write that file again. You will be completely re-writing with old data + new data.


这篇关于过度编写现有的XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆