Read xml string into textbox with newline

The textbox control doesn't interpret escapes and neither does XML. so this is just the characters \ r and n. You need to process the string, substituting the string @"\r\n" for "\r\n".

Address.Text = Employee.Address.Replace(@"\r\n", "\r\n");

this is the equivalent to this

Address.Text = Employee.Address.Replace("\\r\\n", "\r\n");

because @ prevents interpreting of \ in the string.

edit: based on a comment, this is even better form, it translates to \n on unix, and \r\n on Windows.

Address.Text = Employee.Address.Replace("\\r\\n", Environment.NewLine);

When you say "\n" in source code, the compiler converts the \n to a newline, but when you read your XML you get the two distinct characters \ and n. These aren't treated by the compiler, so won't be converted to a newline. Instead it will be represented in a string as "\\n", i.e. an escaped backslash followed by n.

The simplest way to fix it probably to replace the characters before displaying.

Address.Text = Employee.Address.Replace("\\r", "\r").Replace("\\n", "\n");

The better way would be to make sure that the generated XML data doesn't contain the escaped sequences. These are not part of XML, and should just be normal line breaks.

Tags:

C#

Xml

Winforms