Joe Cook
Joe Cook

Reputation: 71

Write Message to MQ as ASCII using C#

I have a string that is in the form of XML or any string for that matter and I am getting the BOM when it is written to MQ. I was trying to convert the string to ASCII in the C# application but it still showed in the message in MQ. I thought i could use the Encoding method in the MQMessage()

MQQueueManager queueManager = new MQQueueManager();
queue = queueManager.AccessQueue(QueueName,
               MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);
message = strInputMsg;
queueMessage = new MQMessage();
queueMessage.WriteString(message);
queueMessage.Format = MQC.MQFMT_STRING;
queueMessage.Encoding = MQC.MQENC_NATIVE;

queuePutMessageOptions = new MQPutMessageOptions();

queue.Put(queueMessage, queuePutMessageOptions);

I am not sure what the value for the queueMessage.Encoding line should be.

another question will the Encoding to ASCII remove the Bit Object Mark(BOM)?

Upvotes: 2

Views: 1556

Answers (1)

JasonE
JasonE

Reputation: 2026

If you look here: https://www.ibm.com/support/knowledgecenter/SSFKSJ_9.0.0/com.ibm.mq.ref.dev.doc/q111220_.htm

ReadString, ReadLine and WriteString methods convert between Unicode and the character set of the message; see CharacterSet

and

The WriteString method converts from Unicode to the character set encoded in CharacterSet. If CharacterSet is set to its default value, MQC.MQCCSI_Q_MGR, which is 0, no conversion takes place and CharacterSet is set to 1200. If you set CharacterSet to some other value, WriteString converts from Unicode to the alternate value.

So in effect before you call WriteString, you have a unicode string in .NET. The WriteString method converts from that unicode into the CCSID indicated by the CharacterSet property, which defaults to unicode and gives you the funny 2 byte prefix of the byte order mark (BOM). If you set this to e.g. 850, then it will convert from unicode to a single byte ASCII output as you want.

Upvotes: 3

Related Questions