diff options
author | monojenkins <jo.shields+jenkins@xamarin.com> | 2020-02-19 22:06:28 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-19 22:06:28 +0300 |
commit | b5b50eefe7a9558ab6540a44defc45c71308f391 (patch) | |
tree | 6526a4a530077954014990281bf257967292c413 | |
parent | cec38fcfc83c25901a4301383ce4ee12ebebe315 (diff) |
[2019-12] [bcl] Default XmlSerializer stream serialize to UTF8 Encoding (#18908)
* [mcs] Default XmlSerializer stream serialize to UTF8 Encoding
* [bcl] Add XmlSerialize Test to preserve UTF chars in stream
-rw-r--r-- | mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs | 8 | ||||
-rw-r--r-- | mcs/class/System.XML/Test/System.Xml.Serialization/XmlSerializerTests.cs | 13 |
2 files changed, 15 insertions, 6 deletions
diff --git a/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs b/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs index 60bbc316699..7ef8b2ca554 100644 --- a/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs +++ b/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs @@ -429,13 +429,9 @@ namespace System.Xml.Serialization } } - static Encoding DefaultEncoding = Encoding.Default; - public void Serialize (Stream stream, object o) { - XmlTextWriter xmlWriter = new XmlTextWriter (stream, DefaultEncoding); - xmlWriter.Formatting = Formatting.Indented; - Serialize (xmlWriter, o, null); + Serialize (stream, o, null); } public void Serialize (TextWriter textWriter, object o) @@ -452,7 +448,7 @@ namespace System.Xml.Serialization public void Serialize (Stream stream, object o, XmlSerializerNamespaces namespaces) { - XmlTextWriter xmlWriter = new XmlTextWriter (stream, DefaultEncoding); + XmlTextWriter xmlWriter = new XmlTextWriter (stream, Encoding.UTF8); xmlWriter.Formatting = Formatting.Indented; Serialize (xmlWriter, o, namespaces); } diff --git a/mcs/class/System.XML/Test/System.Xml.Serialization/XmlSerializerTests.cs b/mcs/class/System.XML/Test/System.Xml.Serialization/XmlSerializerTests.cs index 61835e709a0..e87df12964e 100644 --- a/mcs/class/System.XML/Test/System.Xml.Serialization/XmlSerializerTests.cs +++ b/mcs/class/System.XML/Test/System.Xml.Serialization/XmlSerializerTests.cs @@ -2676,6 +2676,19 @@ namespace MonoTests.System.XmlSerialization Assert.AreEqual ("<:GenComplexStructOfInt32String http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:something>123</><:simpleclass><:something>456</></><:simplestruct><:something>789</></><:listclass><:somelist><:int>100</><:int>200</></></><:arrayclass><:arr><:int>11</><:int>22</><:int>33</></></><:twoclass><:something1>10</><:something2>Ten</></><:derivedclass><:something1>two</><:something2>2</><:another1>1</><:another2>one</></><:derived2><:something1>4</><:something2>four</><:another1>3</><:another2>three</></><:nestedouter><:outer>5</></><:nestedinner><:inner>six</><:something>6</></></>", WriterText); } + [Test] + public void TestSerializeStreamPreserveUTFChars () { + string foo = "BÄR"; + XmlSerializer serializer = new XmlSerializer (typeof (string)); + + MemoryStream stream = new MemoryStream (); + + serializer.Serialize (stream, foo); + stream.Position = 0; + foo = (string) serializer.Deserialize (stream); + Assert.AreEqual("BÄR", foo); + } + [Test] // bug #80759 public void HasNullableField () { |