1

I am trying to deserialize a simple Xml Document.

<?xml version="1.0" encoding="utf-8"?>
<data>
   <somedata> Data1\nData2\n </somedata>
</data>

This is my function.

public class DataTry
{
    public static string deserialize()
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(Data));
        TextReader reader = new StreamReader(@"D:\myfile.xml");
        object obj = deserializer.Deserialize(reader);
        Data XmlData = (Data)obj;
        reader.Close();
        return XmlData.somedata;
    }

    [Serializable, XmlRoot("data")]
    public class Data
    {
        [XmlElement("somedata")]
        public string somedata { get; set; }
    }
}

The result i get back is Data1\\nData2\\n. I don't want the newline character \n to be replaced with \\n.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Nejdi Kroi
  • 612
  • 1
  • 6
  • 17
  • `The result i get back is Data1\nData2\n. I don't want the newline character \n to be replaced with \\n.` This is not clear: are you getting your desired result, yet you don't want that ??? – Hugh Aug 05 '15 at 10:38
  • Sorry I edited the question. I am getting Data1\\nData2\\n – Nejdi Kroi Aug 05 '15 at 10:56
  • why don't u just replace '\\' with '\'? – user786 Aug 05 '15 at 10:59
  • @Nejdi where are you getting the double slash? – Daniel A. White Aug 05 '15 at 11:00
  • @ Alex I don't want to replace every escape character. I am trying to use it in a project which has many escape characters. I want to get the string literal as a result of XML deserialization. – Nejdi Kroi Aug 05 '15 at 11:02
  • @DanielA.White when i call the deserialize() method I get back "Data1\\nData2\\n" I want to get back plain text like this "Data1\nData2\n" as it was in the xml file. – Nejdi Kroi Aug 05 '15 at 11:04
  • @Nejdi specifically where? – Daniel A. White Aug 05 '15 at 11:05
  • 2
    `\n` will show as `\\n` in the debugger. Are you *sure* it's changed it? – Charles Mager Aug 05 '15 at 11:32
  • @DanielA.White Specifically when calling the method in a C# winforms app, using MessageBox.Show(DataTry.deserialize()); It prints out Data1\nData2\n, but I want it to print out newline, like this Data1 "should be a newline here" Data2 – Nejdi Kroi Aug 05 '15 at 12:31
  • http://stackoverflow.com/questions/1115459/xml-deserialization-standardising-line-endings-how-to-stop-it-net – Daniel A. White Aug 05 '15 at 12:33
  • @CharlesMager i know it will show \n will show \\n but I wanted to have just a newline not \\n. I know it does this because it gets the data and it displays literally what I have written in my XML document. But I was wondering how to change the default behavior. I didn't want to use Replace() because then I should change every escape character that is in my XML. Anyway thanks for the help. – Nejdi Kroi Aug 05 '15 at 12:42
  • @DanielA.White I just wanted to insert newline in my XML document and after the deserialization print them out in a MessageBox in different lines. Because I have an XML that was not made by me and it contains \n characters. I want to print newlines and tabs whenever a \n or a \t character is in a deserialized string. Thank you for your help – Nejdi Kroi Aug 05 '15 at 12:50

1 Answers1

1

This is behaving as expected.

Your string is Data1\nData2\n. \n here is not a newline character, it is the literal string \n. When you look at this in the debugger, you will see this in its escaped form \\n.

If you want this to actually be a newline, you will have to unescape your string. One way of doing this is do use Regex.Unescape:

var escaped = @"Data1\nData2\n";        
var unescaped = Regex.Unescape(escaped);

See this demo: https://dotnetfiddle.net/VWphvG

Be wary that this method is intended to unescape regular expressions and will therefore unescape more than just the standard characters. You can see some other options in this related question

Community
  • 1
  • 1
Charles Mager
  • 25,735
  • 2
  • 35
  • 45
  • Yes I understand that it is the expected behavior but I needed to change that. Thank you for your help. This was exactly what I needed. – Nejdi Kroi Aug 05 '15 at 13:56