2

My object template, which is deserialized from a hand made XML file contains mixed types and the text can contain line jumps. When I look at the text I can see line jumps are \r\n, but in my deserialized template object, line jumps are \n. How can I keep line jumps as \r\n?

XmlReaderSettings settings = new XmlReaderSettings();
settings.CloseInput = true;
//settings.ValidationEventHandler += ValidationEventHandler;

settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(schema);

StringReader r = new StringReader(syntaxEdit.Text);

Schema.template rawTemplate = null;

using (XmlReader validatingReader = XmlReader.Create(r, settings))
{
    try
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Schema.template));
        rawTemplate = serializer.Deserialize(validatingReader) as Schema.template;
    }
    catch (Exception ex)
    {
        rawTemplate = null;
        string floro = ex.Message + (null != ex.InnerException ? ":\n" + ex.InnerException.Message : "");
        MessageBox.Show(floro);
    }
}
Ahmet
  • 4,310
  • 4
  • 19
  • 21

2 Answers2

2

It seems that this is required behavior by the XML specification and is a "feature" in Microsoft's implementation of the XmlReader (see this answer).

Probably the easiest thing for you to do would be to replace \n with \r\n in your result.

Community
  • 1
  • 1
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
2

That's the behavior mandated by the XML specification: every \r\n, \r or \n MUST be interpreted as a single \n character. If you want to maintain the \r in your output, you have to change it to a character reference (
) as shown below.

public class StackOverflow_7374609
{
    [XmlRoot(ElementName = "MyType", Namespace = "")]
    public class MyType
    {
        [XmlText]
        public string Value;
    }
    static void PrintChars(string str)
    {
        string toEscape = "\r\n\t\b";
        string escapeChar = "rntb";
        foreach (char c in str)
        {
            if (' ' <= c && c <= '~')
            {
                Console.WriteLine(c);
            }
            else
            {
                int escapeIndex = toEscape.IndexOf(c);
                if (escapeIndex >= 0)
                {
                    Console.WriteLine("\\{0}", escapeChar[escapeIndex]);
                }
                else
                {
                    Console.WriteLine("\\u{0:X4}", (int)c);
                }
            }
        }

        Console.WriteLine();
    }
    public static void Test()
    {
        string serialized = "<MyType>Hello\r\nworld</MyType>";
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(serialized));
        XmlSerializer xs = new XmlSerializer(typeof(MyType));
        MyType obj = (MyType)xs.Deserialize(ms);
        Console.WriteLine("Without the replacement");
        PrintChars(obj.Value);

        serialized = serialized.Replace("\r", "&#xD;");
        ms = new MemoryStream(Encoding.UTF8.GetBytes(serialized));
        obj = (MyType)xs.Deserialize(ms);
        Console.WriteLine("With the replacement");
        PrintChars(obj.Value);
    }
}
carlosfigueira
  • 85,035
  • 14
  • 131
  • 171