I modified the sample code to make it easier to quickly turn code into a C # development environment. I also intentionally did not include the use of operators - this is just sample code.
For example, we have the following class that we want to serialize:
public class DataToSerialize { public string Name { get; set; } }
If we try to serialize and deserialize this in the way you describe the line where "The same" is printed, it will not execute (I assume that the code will work on Windows with Environment.NewLine, replace it with "\ r \ n", if it is not):
DataToSerialize test = new DataToSerialize(); test.Name = "TestData" + Environment.NewLine; XmlSerializer configSerializer = new XmlSerializer(typeof(DataToSerialize)); MemoryStream ms = new MemoryStream(); StreamWriter sw = new StreamWriter(ms); configSerializer.Serialize(sw, test); ms.Position = 0; DataToSerialize deserialized = (DataToSerialize)configSerializer.Deserialize(ms); if (deserialized.Name.Equals("TestData" + Environment.NewLine)) { Console.WriteLine("Same"); }
However, this can be eliminated by manually assigning an XmlTextReader to the serializer, with the Normalization property set to false (the one used by default in the serializer is set to true):
DataToSerialize test = new DataToSerialize(); test.Name = "TestData" + Environment.NewLine; XmlSerializer configSerializer = new XmlSerializer(typeof(DataToSerialize)); MemoryStream ms = new MemoryStream(); StreamWriter sw = new StreamWriter(ms); configSerializer.Serialize(sw, test); ms.Position = 0; XmlTextReader reader = new XmlTextReader(ms); reader.Normalization = false; DataToSerialize deserialized = (DataToSerialize)configSerializer.Deserialize(reader); if (deserialized.Name.Equals("TestData" + Environment.NewLine)) { Console.WriteLine("Same"); }
What will be printed now, if I am wrong, is this the behavior you need?
RichardOD Jul 12 '09 at 9:36 2009-07-12 09:36
source share