private void writeXMLItems(XMLItem rootItem, Values userValues, Values defaultValues) { foreach (KeyValuePair<String, StringValue> kvValue in userValues) { // // get the user values String userValue = kvValue.Value.Value; Values childUserValues = (Values)kvValue.Value; // // get the default values String defaultValue = null; Values childDefaultValues = null; if (defaultValues != null && defaultValues.ContainsKey(kvValue.Key)) { defaultValue = defaultValues[kvValue.Key].Value; childDefaultValues = (Values)defaultValues[kvValue.Key]; } // // compare user and default values if (childUserValues.Count == 0 && String.Compare(userValue, defaultValue) == 0) { continue; } // // write the user values String name = kvValue.Key; try { int.Parse(kvValue.Key); name = "item"; } catch (FormatException) { } // XMLItem xmlItem = new XMLItem(name); if (kvValue.Value != null && kvValue.Value.Value != null && kvValue.Value.Value != String.Empty) { xmlItem.Value = new Value(kvValue.Value.Value); } rootItem.Add(xmlItem); // this.writeXMLItems(xmlItem, childUserValues, childDefaultValues); } }
public void save() { if (!this._xmlFile.Directory.Exists) { this._xmlFile.Directory.Create(); } this._xmlFile.Delete(); // FileStream fStream = null; try { fStream = new FileStream(this._xmlFile.FullName, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter strWrt = new StreamWriter(fStream); // XMLItem rootItem = new XMLItem("settings"); this.writeXMLItems(rootItem, this, (Values)this._defaultSettings); strWrt.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); strWrt.Write(rootItem.ToString()); // rootItem = null; strWrt.Close(); strWrt = null; //} catch(Exception ex) { // throw ex; } finally { if(fStream != null) { fStream.Close(); fStream = null; } } }
private void readXMLItems(XMLItem rootItem, Values parent) { foreach (XMLItem item in rootItem) { StringValue newValue = null; // String name = item.TagName; if (String.Compare(name, "item", true) == 0) { name = rootItem.IndexOf(item).ToString(); } // if(item.Value != null) { String value = item.Value.ToString(); newValue = new StringValue(name, value); } // this.readXMLItems(item, (Values)newValue); // parent.Add(name, newValue); } }