0

I have an object which I would like to store in XML format in an MSMQ queue. What I expect it to appear in the queue body is this:

<?xml version="1.0"?>
<Object>
    <Text>Hello World!</Text>
    <Number>5</Number>
</Object>

However, when I store the object, I would see a diferent result in the body:

3C 3F 78 6D 6C 20 76 65 <?xml ve
72 73 69 6F 6E 3D 22 31 rsion="1
2E 30 22 3F 3E 0D 0A 3C .0"?>..<
4F 62 6A 65 63 74 3E 0D Object>.
0A 09 3C 54 65 78 74 3E .<Text>H
48 65 6C 6C 6F 20 57 6F ello Wor
72 6C 64 21 3C 2F 54 65 ld!</tex
78 74 3E 0D 0A 09 3C 4E t>..<Num 
75 6D 62 65 72 3E 35 3C ber>5</N
2F 4E 75 6D 62 65 72 3E umber>..
0D 0A 3C 2F 4F 62 6A 65 </Object
63 74 3E                >

I'm not sure what the actual format used here is, so I can't workout how I would alter it. I have tried adding messageQueue.Formatter = new BinaryMessageFormatter(); and messageQueue.Formatter = new XmlMessageFormatter(); to the MessageQueue object, but I get exactly the same result.

My code for sending the object is:

// This part of the code of creating the TestObject isn't what I'm using in development, 
// this is just for showing you that I'm using an object.
TestObject item = new TestObject();
item.Text = "Hello World!";
item.Number = 5;

MessageQueue messageQueue;
Message message;

messageQueue = new MessageQueue(@".\Private$\myqueue");
message = new Message(item);

messageQueue.Send(message);

How would I alter my code to make sure it sends and stores in an XML format?

Any help would be appreciated.

Sam Bunting
  • 845
  • 1
  • 15
  • 36
  • The first result shows the output you are after. It simply displays the ASCII vaules to the left. – Emond Oct 26 '17 at 13:47
  • @ErnodeWeerd But would it not be better to have it in readable XML? – Sam Bunting Oct 26 '17 at 13:48
  • "I would see a diferent result in the body" - where exactly do you see this result? – Evk Oct 26 '17 at 13:51
  • How would you "see" that result, and why does it bother you? It looks like a simple presentation issue, where the UI tool you're using to look in the queue doesn't *know* you're storing XML. You could be storing any old binary junk, so it uses the most general presentation format possible. – Jeroen Mostert Oct 26 '17 at 13:51
  • It is in readable XML, look to the right. The viewer you are using is an all-purpose viewer that simply displays the bytes and, for your convenience, text on the right. – Emond Oct 26 '17 at 13:52

1 Answers1

0

The hex dump above almost appears to be the same result you would get if you right-clicked properties in "Computer Management / Message Queuing / Private Queues / myqueue / Queue messages" on a message and selected the "Body" tab. When you do that, it's possible for the viewer to present you with either a string dump or hex dump. Typically a large size presents you with a hex dump.

To have your objects serialized as XML, just set the queue's MessageQueue.Formatter or message's Message.Formatter (for just a particular message) to an XmlMessageFormatter, but include the types you'll need. Then the object will become a serialized XML message. Included below is the code posted with some additions.

// This part of the code of creating the TestObject isn't what I'm using in development, 
// this is just for showing you that I'm using an object.
TestObject item = new TestObject();
item.Text = "Hello World!";
item.Number = 5;

MessageQueue messageQueue;
Message message;
XmlMessageFormatter format = new XmlMessageFormatter(
    new Type[] {
        typeof(string),
        typeof(TestObject)}
);

messageQueue = new MessageQueue(@".\Private$\myqueue");
message = new Message(item);
message.Formatter = format;

messageQueue.Send(message);

// ...

message = messageQueue.Receive();
Type messageType = message.Body.GetType();
if (messageType == typeof(string))
{
    string newString = (string)message.Body;
}
else if (messageType == typeof(TestObject))
{
    TestObject receiveItem = (TestObject)message.Body;
}
J. Hanney
  • 29
  • 4