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.
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”.
this is the equivalent to this
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.