XML Deserialization C#, newline character \n is replaced with \\n -
i trying deserialize simple xml document.
<?xml version="1.0" encoding="utf-8"?> <data> <somedata> data1\ndata2\n </somedata> </data>
this 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 data1\\ndata2\\n. don't want newline character \n replaced \\n.
this behaving expected.
your string data1\ndata2\n
. \n
here not newline character, literal string \n
. when @ in debugger, see in escaped form \\n
.
if want newline, have unescape string. 1 way of doing use regex.unescape
:
var escaped = @"data1\ndata2\n"; var unescaped = regex.unescape(escaped);
see demo: https://dotnetfiddle.net/vwphvg
be wary method intended unescape regular expressions , therefore unescape more standard characters. can see other options in this related question
Comments
Post a Comment