Hi,
I had some trouble getting newlines to serialize across web services. I then read that a web service in .NET 2.0 adheres to a certain soap/xml standard wherein each \r\n character is serialized as \n. I've already written a string substitute class that overrides its own serialization and replaces the unix newlines (\n) back to Environment.Newline (\r\n).
I was wondering if there is a better (cleaner) way to do this, e.g. some setting or tweak on the way objects are (de)serialized. The WS does not need full interoperability, so anything that would limit it to be used only on the .NET platform would be ok too.
thanks,
Stephane

Transporting a newline character (\r\n) across a web service
Ken Davis
Karim ElDeeb
Hi Stephane
I tried retrieving a string "manish\r\n" and when i try to get the ouput of the web service in my application it comes out to be manish only as while serializing Web Service converts \r\n to \n.
However if i try retrieving \\r\\n . the returned output will be \r\n . I have noy used any class overriding for serialization.
Please see if it solves your problem..
Kevin Gray
We're digging up an old thread here, but I'll chime in since this is something that really bugs me.
One of our web services has fields that are sometimes transported in plain text over soap, but sometimes encrypted and Base64 encoded instead. To allow for this situation, I find it is preferable to do the replacement like this:
private string useCrLf(string str)
{
str = str.Replace("\r\n", "\n");
str = str.Replace("\r", "\n");
str = str.Replace("\n", "\r\n");
return str;
}
That should handle any common line endings. Of course, using a Regex.Replace might be more efficient.
Haeal
Hi Stephane,
I understood it. when client is receiving back the response.All \r \n has been converted into \n. That is fine as during serialization it get chnaged.
1. However stephane although it gets change, it's wotk to add a new line character will temain same.
E.g
if i transferred manish \r\n kumar\r\n dudeja \\r\\n
output which my client will received : manish \n\ kumar\n dudeja\\r\\n
If i will show this output ina text box it will be like that:
manish
kumar
dudeja \\r\\n.
So i mean to say the function of newline character has not been chnaged.
2. As you mentioned, user can enter free text then suppose he enters \r\n and it shoudl be treated as "characters" rather that new line characters then i will change it to \\r\\n. And upon receiving back response from server I know that I have replaced \r\n to \\r\\n. So I will change it back to \r\n just before showing that back to user.
Please note that in whole scenarion, I have not used SoapExtension, Soaprequest or Soapresponse to override the behaviour of Soap message. everything has been donw before sending the Soap request and after receiving Soap response.
Onder
if we look at what you send to the webservice in you application, there is a difference between what you fill in, and what is received at the other end.
You send:
manish \r\n kumar \r\n dudeja \\r\\n
The web service receives (and sends back):
manish \n kumar \n dudeja \\r\\n
Now if we do the same without sending the text ourselves first, the following happens
The web service sends:
manish \r\n kumar \r\n dudeja \\r\\n
The client receives:
manish \n kumar \n dudeja \\r\\n
Because \r\n is always being serialized as \n.
My users fill in a multiline text box, like you do. They enter some text that will be put into a letter. They want the letter to contain the line breaks they put into the text box. Parts of the letter are dynamic. These will be filled in by the server.
The user fills in his/her free text, selects some data to be put in the letter as well (e.g. from a combobox). This is then sent to the server for processing. The server puts in the dynamic content (i.e. replaces the id from the combobox with some other text) and sends it back to the client.
When it comes back to the client, all \r\n have been replaced by \n, thus the layout of the letter is no longer the same (all the line breaks are gone).
I need a way to send stuff over webservices without this happening.
thanks,
Stephane
Alex Chew
also looking for a solution to this!
i guess it has something to do with the encoding of xml charset or something
Sniffy
It would seem there is no built-in way of dealing with this behaviour. However, it is save to presume that text returned from a web service will always use \n, as opposed to \r\n. I understand that this is a problem, because the .NET TextBox control uses \r\n only, and \n will show up as an unrecognised character (a square box character in most cases).
Now, this was probably your first thought, but my prefered solution is to simply use the String.Replace(string oldValue, string newValue) method. If you use this when populating a MultiLine TextBox (from the web service output), you should be safe to do so because this behaviour is predictable.
I agree with everyone's instant response that this is a little hacky - but it seems to be the only workaround. Please post your suggestions if you have any better ideas!
Mike Morrison
Basel Alnachef
Hi Stephane
here is what i tried at my end to get the scenario mentioned by you.
My web Service's work is to send back the string which i will pass to it.
I have a winform application with a multiline text box now if put
manish
kumar
dudeja /r/n
in my multiline textbox and pass it to web service then my data passed as a string is:
manish \r\n kumar \r\n dudeja \\r\\n . Although the user entered \r\n in my multiline text box . I am getting the \\r\\n text box.
if you want line breaks then u will get \r\n only back from server. I could not understand where we need \r\n back from server, if we need it then we will pass \\r\\n back to server. We can work with both the things .
If we want to separate \r from \n, then we will pass \\r \n only.
Do let me know if it solves ur purpose or may be I am not getting the exact problem(Then Please write the scenario in details).
Nam Tran
My usage scenario is this: a user fills in a form in a winforms client. Part of it can be free text, and this can include line breaks. The data in the form (including the free text with line breaks) is transported to the server over a web service. This is then processed and sent back to the client. The line breaks must still be present after processing.
If I start using \\r\\n instead of newlines, I'll still need to replace this with Environment.Newline before showing it to the user, because I want him/her to see line breaks and not \\r\\n or \r\n on the screen.
Thanks for your suggestion, it's good but it just doesn't work for this usage scenario, I think.
Stephane