Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read xml string into textbox with newline

Tags:

c#

xml

winforms

I have a class with a member "Address" that I read from an XML file that has the following format(fake address - The XML field name is "Address" - not shown):

106-1190 Crescent St. \r\n Toronto Ont \r\n V2K 2Z6

I have created a Multi-Line Textbox to display in a form with the Accepts-Return property set to true.

The Textbox displays the following with the new-line characters not working

106-1190 Crescent St. \r\n Toronto Ont \r\n V2K 2Z6

I've looked online for various solutions and I have tried the following:

1) \n and \\n

2) \\r\\n

It works if I add to the property directly like so:

Address.Text = "106-1190 Crescent St.\r\nToronto Ont\r\nV2K 2Z6";

But if I load a string variable from an XML file and try to assign it to the Text property:

Address.Text = Employee.Address;

It does not work.

I set watch points to check the difference between the direct assignment and the variable assignment ang got this difference:

Direct: 106-1190 Crescent St. \r\n Toronto Ont \r\n V2K 2Z6

Variable: 106-1190 Crescent St. \\r\\n Toronto Ont \\r\\n V2K 2Z6

so somewhere the code is changing the backslash to a double backslash in converting from the XML string to the variable.

I don't really want to have to parse my XML strings to remove the extra slash.... How do I represent my string data in an XML file such that it gets converted to a proper string variable with just the single slash, in order for my line breaks to show up properly.

Thanks in advance.

like image 893
Greycrow Avatar asked Feb 06 '10 20:02

Greycrow


2 Answers

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);
like image 52
John Knoeller Avatar answered Oct 11 '22 15:10

John Knoeller


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.

like image 43
Jakob Borg Avatar answered Oct 11 '22 16:10

Jakob Borg