Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Services > \r\n Problem

Reply
Thread Tools

\r\n Problem

 
 
Dragan
Guest
Posts: n/a
 
      01-17-2008
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?
 
Reply With Quote
 
 
 
 
John Saunders [MVP]
Guest
Posts: n/a
 
      01-20-2008
"Dragan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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).
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer


 
Reply With Quote
 
 
 
 
Dragan
Guest
Posts: n/a
 
      01-20-2008


"John Saunders [MVP]" wrote:

> "Dragan" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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).
> --
> --------------------------------------------------------------------------------
> John Saunders | MVP - Windows Server System - Connected System Developer
>
>
>



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);

 
Reply With Quote
 
John Saunders [MVP]
Guest
Posts: n/a
 
      01-21-2008
"Dragan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>
> "John Saunders [MVP]" wrote:
>
>> "Dragan" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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).
>> --
>> --------------------------------------------------------------------------------
>> John Saunders | MVP - Windows Server System - Connected System Developer
>>
>>
>>

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

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


 
Reply With Quote
 
Dragan
Guest
Posts: n/a
 
      01-21-2008

> 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
>
>
>


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

 
Reply With Quote
 
John Saunders [MVP]
Guest
Posts: n/a
 
      01-21-2008
"Dragan" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
>
>> 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
>>
>>
>>

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


 
Reply With Quote
 
Dragan
Guest
Posts: n/a
 
      01-23-2008
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?

 
Reply With Quote
 
John Saunders [MVP]
Guest
Posts: n/a
 
      01-23-2008
"Dragan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer


 
Reply With Quote
 
Dragan
Guest
Posts: n/a
 
      01-23-2008
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments