Groups | Blog | Home
all groups > asp.net webservices > january 2008 >

asp.net webservices : \r\n Problem


Dragan
1/17/2008 6:01:03 AM
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.

John Saunders [MVP]
1/20/2008 8:49:15 AM
[quoted text, click to view]

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).
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer

Dragan
1/20/2008 9:09:00 AM


[quoted text, click to view]


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);
Dragan
1/21/2008 7:51:02 AM

[quoted text, click to view]

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
John Saunders [MVP]
1/21/2008 8:16:57 AM
[quoted text, click to view]

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[i])));
}

Try that and see what the result is.
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer

John Saunders [MVP]
1/21/2008 11:41:05 AM
[quoted text, click to view]

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.
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer

Dragan
1/22/2008 5:02:02 PM
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?
Dragan
1/22/2008 5:29:05 PM
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
John Saunders [MVP]
1/22/2008 8:08:10 PM
[quoted text, click to view]

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.
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer

AddThis Social Bookmark Button