C# – base64 and UTF-8 encoding issue


I am writing a simple web method which returns byte[], and the byte[] is encoded by UTF-8. I have investigated related WSDL and soap message, seems the underlying web services stack will use base64 encoding?

For various reasons, I can not use or re-encode my return byte[] from UTF-8 to base64. Any ideas to modify the base64 encoding to UTF-8 encoding?

Here is my related web methods, SOAP message and related type in WSDL

Web service server side code

        public byte[] HelloWorld2()
            return utf8encodedbytes;

SOAP response

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <HelloWorld2Response xmlns="http://tempuri.org/">

related type in WSDL


thanks in advance,

Best Solution

Your question doesn't really make a lot of sense I'm afraid - byte arrays aren't encoded with text encodings like UTF-8. You normally apply a UTF-8 encoding to text to get a byte array. To transfer arbitrary byte arrays in XML you should then use base64.

If you want to transfer text you should just return a string to start with.

What is the original data, before any kind of encoding happens? If it's a byte array, you should return a byte array and the XML should automatically contain base64-encoded data. If it's text, you should return a string and let XML cope with the encoding.