summaryrefslogtreecommitdiff
blob: b306b697c09df1f6501c55bf907e168b9ae501c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
diff --git a/mcs/class/System/System.Configuration/CustomizableFileSettingsProvider.cs b/mcs/class/System/System.Configuration/CustomizableFileSettingsProvider.cs
index 4b69010..1bb6195 100644
--- a/mcs/class/System/System.Configuration/CustomizableFileSettingsProvider.cs
+++ b/mcs/class/System/System.Configuration/CustomizableFileSettingsProvider.cs
@@ -578,6 +578,34 @@ namespace System.Configuration
 		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);
@@ -623,7 +651,7 @@ namespace System.Configuration
 					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;