Search code examples
c#utf-8tibcoems

Sending UTF-8 Text Messages on C# using TIBCO EMS


I'm using the TIBCO EMS library TIBCO.EMS.dll to send xml messages to a queue on a TIBCO EMS server. The application receiving those messages requires the XML to be UTF-8 encoded. Generating the UTF-8 xml in itself isn't a problem, however I can see no method of sending a TextMessage to the queue while keeping the data in UTF-8 format.

To serialize the objects to UTF-8 XML I use the following (simplified here):

XmlSerializer serializer = new XmlSerializer(data.GetType());
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms, System.Text.Encoding.UTF8);
serializer.Serialize(sw, data);
byte[] result = ms.ToArray();

Which leaves me with a byte array containing the utf-8 encoded xml. I can write this to a BytesMessage to send to the EMS queue..

BytesMessage message = _queueSession.CreateBytesMessage();
message.WriteBytes(result);
_queueSender.Send(message);
_queueSession.Commit();

But that results in a BytesMessage on the queue. The only way I can see to get a TextMessage is to use the TextMessage class but the text property of that class is a standard Unicode string which would result in the xml loosing its utf-8 encoding.

Anyone know of a way to send a UTF-8 encoded text message?


Solution

  • It seems that, by default, the TIBCO API converts C# unicode strings to UTF-8 when a message is submitted to a queue. Fine for text, but if the string uses XML and include an encoding type option, you must manually change the option to utf-8.