\r\n Problem

D

Dragan

I'm calling a web service that returns a sting.
The string contains \r\n. This value is converted to \n in the client
application.

Any solutions?
 
J

John Saunders [MVP]

Dragan said:
I'm calling a web service that returns a sting.
The string contains \r\n. This value is converted to \n in the client
application.

Any solutions?

First, be really sure that it is actually returning \r\n (don't just look at
the code - look at what is actually returned). Then, look at the XML that
gets sent to the client. See how that differs from what you expected.

Then, maybe you should write a small set of programs that reproduce the
problem, and post it all here (client, service, wsdl).
 
D

Dragan

John Saunders said:
First, be really sure that it is actually returning \r\n (don't just look at
the code - look at what is actually returned). Then, look at the XML that
gets sent to the client. See how that differs from what you expected.

Then, maybe you should write a small set of programs that reproduce the
problem, and post it all here (client, service, wsdl).


Thank for your reply,

I have notice that the problem occurs when I return a string from
Exception.Message.

The web service code:

[WebMethod]
public string ValidateAmount(string amount)
{
string errorMessage;
try
{
if (amount == null)
throw new ArgumentException("Argument cannot be empty", "Amount");
else
errorMessage = "No Error";
}
catch (Exception ex)
{
errorMessage = ex.Message;
}

return errorMessage;
}


The client code:

string amount = null;
Service1 NewLineService = new Service1();
tbResult.Text = NewLineService.ValidateAmount(amount);
 
J

John Saunders [MVP]

Dragan said:
John Saunders said:
First, be really sure that it is actually returning \r\n (don't just look
at
the code - look at what is actually returned). Then, look at the XML that
gets sent to the client. See how that differs from what you expected.

Then, maybe you should write a small set of programs that reproduce the
problem, and post it all here (client, service, wsdl).


Thank for your reply,

I have notice that the problem occurs when I return a string from
Exception.Message.

The web service code:

[WebMethod]
public string ValidateAmount(string amount)
{
string errorMessage;
try
{
if (amount == null)
throw new ArgumentException("Argument cannot be empty",
"Amount");
else
errorMessage = "No Error";
}
catch (Exception ex)
{
errorMessage = ex.Message;
}

return errorMessage;
}


The client code:

string amount = null;
Service1 NewLineService = new Service1();
tbResult.Text = NewLineService.ValidateAmount(amount);

An even better way to (maybe) reproduce the problem would be:

[WebMethod]
public string ValidateAmount(string amount)
{
return "\r\n";
}

Then, in your client:

Service1 NewLineService = new Service1();
string result = NewLineService.ValidateAmount(null);
Trace.WriteLine(result.Length.ToString());
for (int i=0; i<result.Length; i++)
{
Trace.WriteLine(String.Format("Char {0}={1:X}", i,
Convert.ToUInt16(result)));
}

Try that and see what the result is.
 
D

Dragan

An even better way to (maybe) reproduce the problem would be:

[WebMethod]
public string ValidateAmount(string amount)
{
return "\r\n";
}

Then, in your client:

Service1 NewLineService = new Service1();
string result = NewLineService.ValidateAmount(null);
Trace.WriteLine(result.Length.ToString());
for (int i=0; i<result.Length; i++)
{
Trace.WriteLine(String.Format("Char {0}={1:X}", i,
Convert.ToUInt16(result)));
}

Try that and see what the result is.


I have executed your sample code and the output was:
1
Char 0=A

But when I copy the ValidateAmount method to the client application (not in
a web service), everything is working properly and the output is:
2
Char 0=D
Char 1=A
 
J

John Saunders [MVP]

Dragan said:
An even better way to (maybe) reproduce the problem would be:

[WebMethod]
public string ValidateAmount(string amount)
{
return "\r\n";
}

Then, in your client:

Service1 NewLineService = new Service1();
string result = NewLineService.ValidateAmount(null);
Trace.WriteLine(result.Length.ToString());
for (int i=0; i<result.Length; i++)
{
Trace.WriteLine(String.Format("Char {0}={1:X}", i,
Convert.ToUInt16(result)));
}

Try that and see what the result is.


I have executed your sample code and the output was:
1
Char 0=A

But when I copy the ValidateAmount method to the client application (not
in
a web service), everything is working properly and the output is:
2
Char 0=D
Char 1=A


Excellent. You have now shown that there _is_ a problem, and you're close to
finding out exactly _what_ the problem is. It's always better to narrow
things down to the smallest example that demonstrates the problem.

Note: there are parts of XML that use the concepts of significant whitespace
and insignificant whitespace. To narrow this down just a little further, try
the same experiment with " \r\n ". I'm betting you'll get 20 20 20 0A 20
20 20. That is, I want to see if the problem is specific to newline handling
or whether it is an issue with whitespace in general.

I suspect that you may need to serialize your string as base64Binary or
hexBinary if you want it preserved exactly.
 
D

Dragan

Yes, you are right, the output was:

7
Char 0=20
Char 1=20
Char 2=20
Char 3=A
Char 4=20
Char 5=20
Char 6=20

Now how to set ‘\r\n’ as a significant whitespace?
 
J

John Saunders [MVP]

Dragan said:
Yes, you are right, the output was:

7
Char 0=20
Char 1=20
Char 2=20
Char 3=A
Char 4=20
Char 5=20
Char 6=20

Now how to set '\r\n' as a significant whitespace?

As I said, I'm not sure it's possible without serializing your string as
base64Binary or hexBinary. Look in the MSDN documentation for the
XmlElementAttribute class, and the DataType property.
 
D

Dragan

Yes you are right the output is:

7
Char 0=20
Char 1=20
Char 2=20
Char 3=A
Char 4=20
Char 5=20
Char 6=20
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top