c# XML serialize then deserialize an object with unicode characters

ziyyadyousef

New Member
I have with Unicode properties that I want to serialize to a file. I've written a test to simulate what happens. \[code\] public class TestClassWithUnicode { public string TestProperty = "\x1B"; } [TestMethod] public void TestSerializationOfUnicode() { TestClassWithUnicode tc = new TestClassWithUnicode(); var outStream = new StringWriter(); var s = new XmlSerializer(tc.GetType()); var ns = new XmlSerializerNamespaces(); s.Serialize(outStream, tc, ns); string xml = outStream.ToString(); var xs = new XmlSerializer(tc.GetType()); TestClassWithUnicode tc2 = (TestClassWithUnicode)xs.Deserialize(new StringReader(xml)); }\[/code\]The test will fail:\[code\]Test method TestSerializationOfUnicode threw exception: System.InvalidOperationException: There is an error in XML document (3, 20). ---> System.Xml.XmlException: '', hexadecimal value 0x1B, is an invalid character. Line 3, position 20.\[/code\]What is the correct way to accomplish this? Thanks
 
Back
Top