views:

1313

answers:

2

To show this fundamental issue in .NET and the reason for this question, I have written a simple test web service with one method (EditString), and a consumer console app that calls it.

They are both standard web service/console applications created via File/New Project, etc., so I won't list the whole code - just the methods in question:

Web method:

    [WebMethod]
    public string EditString(string s, bool useSpecial)
    {
        return s + (useSpecial ? ((char)19).ToString() : "");
    }

[You can see it simply returns the string s if useSpecial is false. If useSpecial is true, it returns s + char 19.]

Console app:

        TestService.Service1 service = new SCTestConsumer.TestService.Service1();

        string response1 = service.EditString("hello", false);
        Console.WriteLine(response1);

        string response2 = service.EditString("hello", true); // fails!
        Console.WriteLine(response2);

[The second response fails, because the method returns hello + a special character (ascii code 19 for argument's sake).]

The error is:

  • There is an error in XML document (1, 287)

  • Inner exception: "'', hexadecimal value 0x13, is an invalid character. Line 1, position 287."

A few points worth mentioning:

  • The web method itself WORKS FINE when browsing directly to the ASMX file (e.g. http://localhost:2065/service1.asmx), and running the method through this (with the same parameters as in the console application) - i.e. displays XML with the string hello + char 19.

  • Checking the serialized XML in other ways shows the special character is being encoded properly (the SERVER SIDE seems to be ok which is GOOD)

  • So it seems the CLIENT SIDE has the issue - i.e. the .NET generated proxy class code doesn't handle special characters

  • This is part of a bigger project where objects are passed in and out of the web methods - that contain string attributes - these are what need to work properly. i.e. we're de/serializing classes.

Any suggestions for a workaround and how to implement it?

Or have I completely missed something really obvious!!?

Thanks in advance...

PS. I've not had much luck with getting it to use CDATA tags (does .NET support these out of the box?).

A: 

You will need to use byte[] instead of strings.

John Saunders
Will give it a try, and thanks for the rapid response! Could be a neat solution - just means we have to watch the encoding? If the XML is in say UTF8 - safe bet that's the encoding to use for the byte array? (I would want to allow unicode characters.)
You don't have to worry about the encoding. The serializer will encode it as base64 on your behalf. No worries.
John Saunders
A: 

I am thinking of some options that may help you. You can take the route using html entities instead of char(19). or as you said you may want to use CDATA.

To come up with a clean solution, you may not want to put the whole thing in CDATA. I am not sure why you think it may not be supported in .NET. Are you saying this in the context of serialization?

CodeToGlory
Yes in the context of serialization - which is all handled behind the scenes by the .NET web service/and the proxy class. I didn't really want to get my hands dirty with (or think it should be necessary) the serialization and there'd just be an xml attribute I could use to make it serialise a CDATA tag around my string... The data is product descriptions coming from a database for a website, which has been known to contain special characters, and I just want my web service to handle it. (This example just shows out of the box .NET falls over seemingly without extra witchcraft...)
did you try using the html entities route? It can get complex if you need to hunt down lots of these.
CodeToGlory