使用数据集写入xml而不覆盖现有数据

时间:2021-09-07 20:26:28

I'm using a DataSet to represent the settings I have in my project. More or less the code is as follows:

我正在使用DataSet来表示我在项目中的设置。或多或少的代码如下:

if (!ValidateSettings(SettingsPath)) //returns false if the settingsfile doesn't consist with the DataSet
{
  dsSettings defaultSettings = new dsSettings();
  defaultSettings.ReadXml("settingsTemplate.xml", IgnoreSchema);
  dsSettings.WriteXml(SettingsFilePath);
}

If I run this code it will copy all values in the template file and write those to the SettingsFilePath file (overwriting the content in that file).

如果我运行此代码,它将复制模板文件中的所有值并将其写入SettingsFilePath文件(覆盖该文件中的内容)。

However, in the future, let's say that I add a setting to the DataSet. Then I want the old settings (maybe edited by the user) to remain and only add the missing setting in the xml file from the template file.

但是,在将来,假设我向DataSet添加了一个设置。然后,我希望保留旧设置(可能由用户编辑),并仅从模板文件中添加xml文件中缺少的设置。

Can this be done with some option or something to the WriteXml function or anything as simple as that. Or will I have to read the existing settings file and save each value, write the xml and the overwrite with those saved values?

这可以通过WriteXml函数的一些选项或其他东西来完成,或者像这样简单。或者我是否必须读取现有的设置文件并保存每个值,使用这些保存的值写入xml和覆盖?

1 个解决方案

#1


1  

Every time you call WriteXML the XML created in one block of Valid XML data. In the sense it will contain one root element. So another call means another XML block, if we keep both together we get an invalid XML document since there won't be a root node. That is why you can not acheive what you want that very simply.

每次调用WriteXML时,都会在一个Valid XML数据块中创建XML。从某种意义上说,它将包含一个根元素。所以另一个调用意味着另一个XML块,如果我们将它们放在一起,我们会得到一个无效的XML文档,因为没有根节点。这就是为什么你不能简单地实现你想要的东西。

Having said that you can try a workaround:

说过你可以尝试一种解决方法:

  1. Save as many files as you want in a designated folder. Then read them all into datatables and merge them together into one datatable.

    在指定的文件夹中保存任意数量的文件。然后将它们全部读入数据表并将它们合并为一个数据表。

  2. Create your own single XML file. To do this, use WriteXML method to write the XML to a StringWriter. Merge such XML blocks into one XML Block by creating your own root level node then write them to disk. Similarly while readig first read the file extract the XML Blocks from within the root node and then use ReadXML to read from stream.

    创建自己的单个XML文件。为此,请使用WriteXML方法将XML写入StringWriter。通过创建自己的根级别节点将这些XML块合并到一个XML块中,然后将它们写入磁盘。同样,readig首次读取文件时从根节点中提取XML块,然后使用ReadXML从流中读取。

#1


1  

Every time you call WriteXML the XML created in one block of Valid XML data. In the sense it will contain one root element. So another call means another XML block, if we keep both together we get an invalid XML document since there won't be a root node. That is why you can not acheive what you want that very simply.

每次调用WriteXML时,都会在一个Valid XML数据块中创建XML。从某种意义上说,它将包含一个根元素。所以另一个调用意味着另一个XML块,如果我们将它们放在一起,我们会得到一个无效的XML文档,因为没有根节点。这就是为什么你不能简单地实现你想要的东西。

Having said that you can try a workaround:

说过你可以尝试一种解决方法:

  1. Save as many files as you want in a designated folder. Then read them all into datatables and merge them together into one datatable.

    在指定的文件夹中保存任意数量的文件。然后将它们全部读入数据表并将它们合并为一个数据表。

  2. Create your own single XML file. To do this, use WriteXML method to write the XML to a StringWriter. Merge such XML blocks into one XML Block by creating your own root level node then write them to disk. Similarly while readig first read the file extract the XML Blocks from within the root node and then use ReadXML to read from stream.

    创建自己的单个XML文件。为此,请使用WriteXML方法将XML写入StringWriter。通过创建自己的根级别节点将这些XML块合并到一个XML块中,然后将它们写入磁盘。同样,readig首次读取文件时从根节点中提取XML块,然后使用ReadXML从流中读取。