XML descriptorization deserialization "standardization" of line endings, how to stop it? (.NETWORK)

I have a class with a property marked [XmlText] that accepts multi-line input. In my XML file, I checked that the line ending inside the text content infact "\r\n" is the same as the rest of the file.

The code I use for deserialization is:

 XmlSerializer configSerializer = new XmlSerializer(typeof(WorldList)); string file = "test.xml"; if (File.Exists(file)) { using (TextReader r = new StreamReader(file)) { foo = configSerializer.Deserialize(r); } } 

But inside the installer for the [XmlText] value already has "\n" as the end of the line. The main reason this annoys is because the line ending "\n" does not display properly in the TextBox ... I'm not quite sure which part is causing the problems, but I thought that someone here could shed some light on the situation.

+8
c # xml newline xml-serialization
Jul 12 '09 at 7:04
source share
2 answers

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?

+10
Jul 12 '09 at 9:36
source share

If you are serializing , then XmlWriterSettings.NewLineHandling ( Replace ) is worth a try - but it will not help to read. A bit rough, but maybe do it right in the setter:

 private string text; [XmlText] public string Text { get { return text; } set { Regex r = new Regex("(?<!\r)\n"); text = r.Replace(value, "\r\n"); } } 
+4
Jul 12 '09 at 7:14
source share



All Articles