Tuesday, July 18, 2006

Sending string in xmlhttp in correct string encoding format

Sending string in xmlhttp in a correct character encoding format

Yesterday, we (Shani – one of my team-mate, and I) had a strange problem in character encoding format.

We tried to transfer an xml (string in xml format not the XmlDocument Object) from client side to server side by xmlhttp object, and that xml contained a Hebrew characters.

When the xml arrived to the server side we took the string from the Request object in array of bytes format, then we built a string from this array, and loaded it to an xml object (XmlDocument class).

Then, in the xml property of this object we saw that the xml contain a gibberish in the Hebrew text (probably wrong string encoding format). I tried to add the xml header with the correct encoding declaration, and then to load it to an xml object, but it didn't work.

After a lot of thinking and searching I found the mistake.

I always look on the xml after I convert it from array of bytes to string, and only after I create the all string, I tried to change the string's encoding.

I found that maybe I needed to change the encoding in the beginning of the process, and then use the class System.Text.Encoding.

In this class I found a method that gets strings from array of bytes in UT8-8

encoding (8-bit Unicode Transformation Format).

And everything worked perfect, so next time you will want to send a string in "special" encoding format and to get the same string(in the same encoding format), you should use this method…

Example:



2 comments:

Anonymous said...

Hi,
I thought about some other problem...
what would happen if the sending page declared with Hebrew-(Windows) or with Hebrew (Iso-visual) is it still working ?!

Good job.

Avi Wortzel said...

Hi, it's work!
I tried it in a special tester(UTF-8 works good with a lot of formats).