\r\n Problem

Discussion in 'ASP .Net Web Services' started by Dragan, Jan 17, 2008.

  1. Dragan

    Dragan Guest

    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?
     
    Dragan, Jan 17, 2008
    #1
    1. Advertising

  2. "Dragan" <> wrote in message
    news:...
    > 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
     
    John Saunders [MVP], Jan 20, 2008
    #2
    1. Advertising

  3. Dragan

    Dragan Guest

    "John Saunders [MVP]" wrote:

    > "Dragan" <> wrote in message
    > news:...
    > > 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);
     
    Dragan, Jan 20, 2008
    #3
  4. "Dragan" <> wrote in message
    news:...
    >
    >
    > "John Saunders [MVP]" wrote:
    >
    >> "Dragan" <> wrote in message
    >> news:...
    >> > 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)));
    }

    Try that and see what the result is.
    --
    --------------------------------------------------------------------------------
    John Saunders | MVP - Windows Server System - Connected System Developer
     
    John Saunders [MVP], Jan 21, 2008
    #4
  5. Dragan

    Dragan Guest


    > 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.
    > --
    > --------------------------------------------------------------------------------
    > 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
     
    Dragan, Jan 21, 2008
    #5
  6. "Dragan" <> wrote in message
    news:D...
    >
    >> 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.
    >> --
    >> --------------------------------------------------------------------------------
    >> 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
     
    John Saunders [MVP], Jan 21, 2008
    #6
  7. Dragan

    Dragan Guest

    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, Jan 23, 2008
    #7
  8. "Dragan" <> wrote in message
    news:...
    > 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
     
    John Saunders [MVP], Jan 23, 2008
    #8
  9. Dragan

    Dragan Guest

    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
     
    Dragan, Jan 23, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Suurkuusk
    Replies:
    0
    Views:
    4,000
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,179
    Ted Miller
    Sep 13, 2003
  3. Merek
    Replies:
    0
    Views:
    1,980
    Merek
    Dec 3, 2003
  4. Scott Meddows
    Replies:
    1
    Views:
    404
    John Saunders
    Jun 8, 2004
  5. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    558
    Bullschmidt
    May 11, 2004
Loading...

Share This Page