public void SaveXml() { using (var temp = new TempFile()) { var doc = new XmlDocument(); doc.LoadXml("<?xml version='1.0' encoding='utf-8'?><root>This is a test</root>"); RobustIO.SaveXml(doc, temp.Path); // Not a great test, since it captures some non-essential features of how XmlDocument writes its content. // But this is at least a reasonable result to get from doing the above. Assert.That(File.ReadAllText(temp.Path, Encoding.UTF8), Is.EqualTo("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + Environment.NewLine + "<root>This is a test</root>")); } }
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection) { lock (LockObject) { // We need to forget any cached version of the XML. Otherwise, when more than one lot of settings // is saved in the same file, the provider that is doing the save for one of them may have stale // (or missing) settings for the other. We want to write the dirty properties over a current // version of everything else that has been saved in the file. _settingsXml = null; //Iterate through the settings to be stored, only dirty settings for this provider are in collection foreach (SettingsPropertyValue propval in collection) { var groupName = context["GroupName"].ToString(); var groupNode = SettingsXml.SelectSingleNode("/configuration/userSettings/" + context["GroupName"]); if (groupNode == null) { var parentNode = SettingsXml.SelectSingleNode("/configuration/userSettings"); groupNode = SettingsXml.CreateElement(groupName); parentNode.AppendChild(groupNode); } var section = (XmlElement)SettingsXml.SelectSingleNode("/configuration/configSections/sectionGroup/section"); if (section == null) { var parentNode = SettingsXml.SelectSingleNode("/configuration/configSections/sectionGroup"); section = SettingsXml.CreateElement("section"); section.SetAttribute("name", groupName); section.SetAttribute("type", String.Format("{0}, {1}", typeof(ClientSettingsSection), Assembly.GetAssembly(typeof(ClientSettingsSection)))); parentNode.AppendChild(section); } SetValue(groupNode, propval); } Directory.CreateDirectory(UserConfigLocation); RobustIO.SaveXml(SettingsXml, Path.Combine(UserConfigLocation, UserConfigFileName)); } }