4

If you run this code:

public class Program
{
    public class MyClass
    {
        public string Text { get; set; }
    }

    static MyClass myClass = new MyClass();
    static string Desktop = "C:\\Users\\Juan Luis\\Desktop\\";

    static void Main(string[] args)
    {
        myClass.Text = "\r\nhello";
        Console.WriteLine((int)myClass.Text[0]);
        Save(Desktop + "file.xml", myClass);
        myClass = Load(Desktop + "file.xml");
        Console.WriteLine((int)myClass.Text[0]);
        Console.Read();
    }

    private static MyClass Load(string fileName)
    {
        MyClass result = null;
        using (Stream stream = File.Open(fileName, FileMode.Open))
        {
            XmlSerializer xmlFormatter = new XmlSerializer(typeof(MyClass));
            result = (MyClass)xmlFormatter.Deserialize(stream);
        }
        return result;
    }

    private static void Save(string fileName, MyClass obj)
    {
        using (Stream tempFileStream = File.Create(fileName))
        {
            XmlSerializer xmlFormatter = new XmlSerializer(typeof(MyClass));
            xmlFormatter.Serialize(tempFileStream, obj);
        }
    }
}

The output will be 13, 10. The XmlSerializer is removing the carriage return. This is a problem in my case because I need to compare strings for equality in a class that gets serialized and deserialized, and this is causing two strings that are equal before serializing to be unequal after serialized. What would be the best work around?

Edit: After reading answers, this was my solution, in case it will help anyone:

public class SafeXmlSerializer : XmlSerializer
{
    public SafeXmlSerializer(Type type) : base(type) { }

    public new void Serialize(Stream stream, object o)
    {
        XmlWriterSettings ws = new XmlWriterSettings();
        ws.NewLineHandling = NewLineHandling.Entitize;

        using (XmlWriter xmlWriter = XmlWriter.Create(stream, ws))
        {
            base.Serialize(xmlWriter, o);
        }
    }
}
Juan
  • 15,274
  • 23
  • 105
  • 187

1 Answers1

4

I wouldn't call it unreliable exactly: the XmlSerializer strips white space around text inside elements. If it didn't do this then the meaning of XML documents would change according to how you formatted them in the IDE.

You could consider putting the text in a CDATA section, which will preserve the contents exactly. For example, How do you serialize a string as CDATA using XmlSerializer?

Edit: This looks to have a better explanation of where the problem lies, along with a simpler solution - How to keep XmlSerializer from killing NewLines in Strings?

Community
  • 1
  • 1
Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
  • Do you mean that if my text would be `" hello"` it would be serialized as `"hello"`? I just tested it and it did serialize the white spaces. I'll check your link anyway. – Juan Jan 06 '11 at 17:38
  • From memory, it serializes OK, but it's allowed to strip whitespace when it deserializes. – Tim Robinson Jan 06 '11 at 17:43
  • That will solve my problem... but carriage return is and not as stated in that answer. – Juan Jan 06 '11 at 17:58
  • 1
    Is there anything else I need to do to ensure my strings will stay equal after deseralized? – Juan Jan 06 '11 at 18:10