void MergeDictionaries(PListDictionary original, PListDictionary toMerge, bool keepEmpty, bool forceReplace) { foreach (var kvp in toMerge) { IPListElement toMergeElement = kvp.Value; if (toMergeElement is PListDictionary) { var toMergeDic = toMergeElement as PListDictionary; var originalDic = original.Element <PListDictionary>(kvp.Key); if (originalDic == null) { originalDic = new PListDictionary(); original[kvp.Key] = originalDic; } if (forceReplace) { original [kvp.Key] = toMergeDic.Copy(); originalDic = original.Element <PListDictionary> (kvp.Key); } else { MergeDictionaries(originalDic, toMergeDic, keepEmpty, forceReplace); } if (!keepEmpty && originalDic.Count <= 0) { original.Remove(kvp.Key); } } else if (toMergeElement is PListArray) { var toMergeArray = toMergeElement as PListArray; var originalArray = original.Element <PListArray>(kvp.Key); if (originalArray == null) { originalArray = new PListArray(); original[kvp.Key] = originalArray; } if (forceReplace) { original [kvp.Key] = toMergeArray.Copy(); originalArray = original.Element <PListArray> (kvp.Key); } else { MergeArrays(originalArray, toMergeArray, keepEmpty); } if (!keepEmpty && originalArray.Count <= 0) { original.Remove(kvp.Key); } } else { //skip empty entries //TODO should add empty string? if (string.IsNullOrEmpty(toMergeElement.ToString())) { continue; } //add or overwrite existing value original[kvp.Key] = kvp.Value.Copy(); } } }