简体   繁体   English

使用C#将消息以ASCII格式写入MQ

[英]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. 我有一个XML形式的字符串或与此相关的任何字符串,并且在将BOM写入MQ时得到了BOM。 I was trying to convert the string to ASCII in the C# application but it still showed in the message in MQ. 我试图在C#应用程序中将字符串转换为ASCII,但仍在MQ的消息中显示。 I thought i could use the Encoding method in the MQMessage() 我以为我可以在MQMessage()中使用Encoding方法

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. 我不确定queueMessage.Encoding行的值应该是什么。

another question will the Encoding to ASCII remove the Bit Object Mark(BOM)? 另一个问题是将ASCII编码删除位对象标记(BOM)吗?

If you look here: https://www.ibm.com/support/knowledgecenter/SSFKSJ_9.0.0/com.ibm.mq.ref.dev.doc/q111220_.htm 如果您在此处查看: 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; ReadString,ReadLine和WriteString方法在Unicode和消息的字符集之间进行转换。 see CharacterSet 参见CharacterSet

and

The WriteString method converts from Unicode to the character set encoded in CharacterSet. WriteString方法从Unicode转换为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. 如果CharacterSet设置为其默认值MQC.MQCCSI_Q_MGR,该值为0,则不进行任何转换,而CharacterSet设置为1200。如果将CharacterSet设置为其他值,WriteString从Unicode转换为备用值。

So in effect before you call WriteString, you have a unicode string in .NET. 因此,实际上,在调用WriteString之前,.NET中有一个unicode字符串。 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). WriteString方法从该unicode转换为CharacterSet属性指示的CCSID,该属性默认为unicode,并为您提供字节顺序标记(BOM)的有趣的2字节前缀。 If you set this to eg 850, then it will convert from unicode to a single byte ASCII output as you want. 如果将其设置为例如850,则它将根据需要从unicode转换为单字节ASCII输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM