none
Send and Receive MSMQ messages - XML File

    Question

  • Hi Everybody,

    I have a XML Document which i want to send and receive to a MSMQ queue. I am successfully able to Send the XML Document as a Body, but i am not able to retrieve the message even if I set the Formatter as XML Fomatter. Below is the code snippet.

    Code:

    string queuePath = "FormatName:Direct=TCP:154.154.140231.\\PRIVATE$\\questions";
                MessageQueue queue = new MessageQueue(queuePath);
                XmlDocument xmlDoc = new XmlDocument();

                Message msg = new System.Messaging.Message("Example Message Body");
                String strfilename = "C:\\Users\\skothar\\Documents\\Questions.xml";

                xmlDoc.Load(strfilename);

                msg.Body = xmlDoc;
                msg.Formatter = new System.Messaging.XmlMessageFormatter();
                queue.Send(msg, "Force");

               Message RCmsg = new System.Messaging.Message("Example Message Body");
               RCmsg = queue.Peek(); // NOT WORKING
               RCmsg.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(String) });
               queue.Formatter = new XmlMessageFormatter(new Type[] { typeof(String) });

                RCmsg.Formatter = new XmlMessageFormatter(new Type[] { typeof(String) });

                RCmsg = queue.Peek();           //EVEN AFTER SETTING THE FORMATTER ITS NOT WORKING 
                 
               XmlDocument RcXML = new XmlDocument();

                RcXML.Load(RCmsg.Body.ToString());

    Please guide me through to get the solution.

    Thanks,

    Swapnil

    Monday, September 03, 2012 2:09 PM

Answers

  • I haven't actually tried this out, but I believe that you are misconfiguring the XmlMessageFormatter. You are sending as an argument typeof(String), meaning that you expect to retrieve a String encoded inside the XML message body. But in the "sending" part, you are setting the Body to be an XmlDocument, meaning that you are encapsulating an XmlDocument (instead of a String) inside the XML Body.

    You can change the line msg.Body=xmlDoc; to msg.Body=xmlDoc.InnerXml; this will convert the contents of the cmlDoc into a String, which should then be read by the RcXML.Load instruction that you have after receiving the message.

    Monday, September 03, 2012 3:00 PM