Assert.AreEqual ("Server=(local);Initial Catalog=someDb;User Id=someUser;Password=somePassword;Application Name=someAppName;Min Pool Size=5;Max Pool Size=500;Connect Timeout=10;Connection Lifetime=29;",
connString);
}
+
+ [Test]
+ public void BadConfig ()
+ {
+ string xml = @" badXml";
+
+ var file = Path.Combine (tempFolder, "badConfig.config");
+ File.WriteAllText (file, xml);
+
+ try {
+ var fileMap = new ConfigurationFileMap (file);
+ var configuration = ConfigurationManager.OpenMappedMachineConfiguration (fileMap);
+ Assert.Fail ("Exception ConfigurationErrorsException was expected.");
+ } catch (ConfigurationErrorsException e) {
+ Assert.AreEqual (file, e.Filename);
+ }
+
+ }
}
}
private ExeConfigurationFileMap exeMapPrev = null;
private SettingsPropertyValueCollection values = null;
+ /// <remarks>
+ /// Hack to remove the XmlDeclaration that the XmlSerializer adds.
+ /// <br />
+ /// see <a href="https://github.com/mono/mono/pull/2273">Issue 2273</a> for details
+ /// </remarks>
+ private string StripXmlHeader (string serializedValue)
+ {
+ if (serializedValue == null)
+ {
+ return string.Empty;
+ }
+
+ XmlDocument doc = new XmlDocument ();
+ XmlElement valueXml = doc.CreateElement ("value");
+ valueXml.InnerXml = serializedValue;
+
+ foreach (XmlNode child in valueXml.ChildNodes) {
+ if (child.NodeType == XmlNodeType.XmlDeclaration) {
+ valueXml.RemoveChild (child);
+ break;
+ }
+ }
+
+ // InnerXml will give you well-formed XML that you could save as a separate document, and
+ // InnerText will immediately give you a pure-text representation of this inner XML.
+ return valueXml.InnerXml;
+ }
+
private void SaveProperties (ExeConfigurationFileMap exeMap, SettingsPropertyValueCollection collection, ConfigurationUserLevel level, SettingsContext context, bool checkUserLevel)
{
Configuration config = ConfigurationManager.OpenMappedExeConfiguration (exeMap, level);
UserSettingsGroup userGroup = config.GetSectionGroup ("userSettings") as UserSettingsGroup;
bool isRoaming = (level == ConfigurationUserLevel.PerUserRoaming);
-#if true // my reimplementation
-
if (userGroup == null) {
userGroup = new UserSettingsGroup ();
config.SectionGroups.Add ("userSettings", userGroup);
element.Value.ValueXml = new XmlDocument ().CreateElement ("value");
switch (value.Property.SerializeAs) {
case SettingsSerializeAs.Xml:
- element.Value.ValueXml.InnerXml = (value.SerializedValue as string) ?? string.Empty;
+ element.Value.ValueXml.InnerXml = StripXmlHeader (value.SerializedValue as string);
break;
case SettingsSerializeAs.String:
element.Value.ValueXml.InnerText = value.SerializedValue as string;
}
if (hasChanges)
config.Save (ConfigurationSaveMode.Minimal, true);
-
-#else // original impl. - likely buggy to miss some properties to save
-
- foreach (ConfigurationSection configSection in userGroup.Sections)
- {
- ClientSettingsSection userSection = configSection as ClientSettingsSection;
- if (userSection != null)
- {
-/*
- userSection.Settings.Clear();
-
- foreach (SettingsPropertyValue propertyValue in collection)
- {
- if (propertyValue.IsDirty)
- {
- SettingElement element = new SettingElement(propertyValue.Name, SettingsSerializeAs.String);
- element.Value.ValueXml = new XmlDocument();
- element.Value.ValueXml.InnerXml = (string)propertyValue.SerializedValue;
- userSection.Settings.Add(element);
- }
- }
-*/
- foreach (SettingElement element in userSection.Settings)
- {
- if (collection [element.Name] != null) {
- if (collection [element.Name].Property.Attributes.Contains (typeof (SettingsManageabilityAttribute)) != isRoaming)
- continue;
-
- element.SerializeAs = SettingsSerializeAs.String;
- element.Value.ValueXml.InnerXml = (string) collection [element.Name].SerializedValue; ///Value = XmlElement
- }
- }
-
- }
- }
- config.Save (ConfigurationSaveMode.Minimal, true);
-#endif
}
// NOTE: We should add here all the chars that are valid in a name of a class (Ecma-wise),
//#define SPEW
-
using System;
+using System.IO;
+using System.Xml;
+using System.Xml.Serialization;
+using System.Xml.Schema;
using System.Text;
using System.Configuration;
using System.ComponentModel;
Assert.AreEqual ("eclair", holder1.TestKey, "#4");
Assert.AreEqual ("", holder2.TestKey, "#5");
}
+
+ class Settings : ApplicationSettingsBase
+ {
+ [UserScopedSetting]
+ public WindowPositionList WindowPositions {
+ get {
+ return ((WindowPositionList)(this ["WindowPositions"]));
+ }
+ set {
+ this ["WindowPositions"] = value;
+ }
+ }
+ }
+
+ [Serializable]
+ public class WindowPositionList : IXmlSerializable
+ {
+ public XmlSchema GetSchema ()
+ {
+ return null;
+ }
+
+ public void ReadXml (XmlReader reader)
+ {
+ reader.ReadStartElement ("sampleNode");
+ reader.ReadEndElement ();
+ }
+
+ public void WriteXml (XmlWriter writer)
+ {
+ writer.WriteStartElement ("sampleNode");
+ writer.WriteEndElement ();
+ }
+ }
+
+ [Test] //Covers 36388
+ public void XmlHeader ()
+ {
+ try {
+ var settings = new Settings ();
+ settings.Reset ();
+ settings.Save ();
+
+ settings.WindowPositions = new WindowPositionList ();
+
+ settings.Save ();
+ // If Reloads fails then saved data is corrupted
+ settings.Reload ();
+ } catch (ConfigurationErrorsException e) {
+ // Delete corrupted config file so other test won't fail.
+ File.Delete (e.Filename);
+ Assert.Fail ("Invalid data was saved to config file.");
+ }
+ }
}
}