0

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

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

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.

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?

4

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.

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.

于 2012-04-12T11:47:51.937 回答