简体   繁体   English

从msmq读取时的XML反序列化

[英]XML deserialization when reading from msmq

I want to read XML Messages from a Message Queue in a C# WPF Application. 我想从C#WPF应用程序中的消息队列中读取XML消息。 Messages are saved into the Queue by a Navision codeunit. 消息由Navision代码单元保存到队列中。 Firstly, I am not really sure if the messages that are saved in the Queue are usable, because they are in some sort of hexadecimal format which looks like this: 首先,我不确定在队列中保存的消息是否可用,因为它们是某种十六进制格式,如下所示:

FF FE 3C 00 3F 00 78 00 ÿþ<.?.x.
6D 00 6C 00 20 00 76 00 m.l. .v.
65 00 72 00 73 00 69 00 e.r.s.i.
6F 00 6E 00 3D 00 22 00 o.n.=.".
31 00 2E 00 30 00 22 00 1...0.".
20 00 65 00 6E 00 63 00  .e.n.c.
6F 00 64 00 69 00 6E 00 o.d.i.n.
67 00 3D 00 22 00 55 00 g.=.".U.
54 00 46 00 2D 00 31 00 T.F.-.1.
36 00 22 00 20 00 73 00 6.". .s.
74 00 61 00 6E 00 64 00 t.a.n.d.
61 00 6C 00 6F 00 6E 00 a.l.o.n.
...

Receiving the messages from the queue already works, but somehow the format is wrong because I get this Runtime Exception "Invalid Operation Exception: Cannot deserialize the message passed as an argument. Cannot recognize the serialization format." 从队列接收消息已经有效,但是格式错误,因为我得到了这个运行时异常“无效的操作异常:无法反序列化作为参数传递的消息。无法识别序列化格式。”

I am using this code to read the messages: 我正在使用此代码来阅读消息:

public MainWindow()
{
    InitializeComponent();
    mqCustomerData = new MessageQueue(@".\private$\customerData");
    mqCustomerData.Formatter = new XmlMessageFormatter(new Type[] { typeof(String) });
    mqCustomerData.ReceiveCompleted += new ReceiveCompletedEventHandler(mqCustomerData_ReceiveCompleted);
    mqCustomerData.BeginReceive(new System.TimeSpan(0, 0, 0, 30));
}

private void mqCustomerData_ReceiveCompleted(object sender, System.Messaging.ReceiveCompletedEventArgs e)
{
    Message m = new Message();
    m.Formatter = new XmlMessageFormatter(new Type[] { typeof(String) });
    m = mqCustomerData.EndReceive(e.AsyncResult);
    string text = (string)m.Body;
}

I've searched for the problem but not found a useful solution, only found postings of other users experiencing the same problem, like here: http://www.webmasterworld.com/microsoft_asp_net/4119362.htm 我已经搜索了这个问题,但没有找到有用的解决方案,只发现遇到同样问题的其他用户的帖子,例如: http//www.webmasterworld.com/microsoft_asp_net/4119362.htm

I hope someone of you out there can help me with this :) 我希望你们中的某个人可以帮助我:)

In competent_tech's answer, no overload for 在competent_tech的回答中,没有超载

Encoding.Unicode.GetString(m.Body);

takes a string argument. 采用字符串参数。

If this is the approach that you want to use, you need to convert the string to a byte array - 如果这是您要使用的方法,则需要将字符串转换为字节数组 -

byte[] arr;
    using(MemoryStream ms = new MemoryStream())
                    {
                        m.BodyStream.Position = 0;
                        m.BodyStream.CopyTo(ms);
                        arr =  ms.ToArray();
                    }



     string s = Encoding.Unicode.GetString(arr, 0, arr.Length);  

It looks like the data is little-endian UTF-16 since the data starts with FF FE. 看起来数据是小端UTF-16,因为数据以FF FE开头。

You need to decode the string using something like: 您需要使用以下内容解码字符串:

string text = Encoding.Unicode.GetString(m.Body);

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

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