• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 880
  • Last Modified:

MSMQ and XML document

hello experts,
I am sending an XML file to a queue successfully but i am not able to retrieve it...I want to be able to view the file and save the file in another folder.
Please help.
here is the snippet I am using to send the file:

XmlDocument xmlDoc = new XmlDocument();

here is the snippet I am using to retrieve it.
System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue ( textBox1.Text);
System.Xml.XmlDocument XmlDoc = new System.Xml.XmlDocument();
mq.Formatter = new System.Messaging.XmlMessageFormatter (new Type[] {XmlDoc.GetType()}) ;
                      System.Messaging.Message msg = mq.Receive() ;
                       XmlDoc =(System.Xml.XmlDocument)msg.Body;
                       XmlDoc.Save("path to save");
                       MessageBox.show (XmlDoc.ToString());
Many Thanks

  • 2
1 Solution

 The Receive method of MessageQueue class attempts to deserialize the message into objects of types specified in Formatter property. I don't think you have XmlDocument objects serialized in test.xml file, do you?

  So, types indicated in Formatter property should be the types of objects serialized in test.xml file. An example that works would have:

   a serializable class:
          public class Customer
            public int CustomerID;
            public string CustomerName;

   a correct 'test.xml'  file will have a content like:

<?xml version="1.0" encoding="utf-8"?>

  Send the file the way you did, and receive the message like this:
      mq.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] {typeof(Customer)});
      System.Messaging.Message msg = mq.Receive() ;
      Customer c2 =(Customer)msg.Body;


   On the other hand, if you just want to take the content of the message 'as is' and put it into an xml you need no Formatter,  you would just read the 'BodyStream' of the message and write the content into a file, something like:

     System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue ( textBox1.Text);
            //System.Xml.XmlDocument XmlDoc = new System.Xml.XmlDocument();
           // mq.Formatter = new System.Messaging.XmlMessageFormatter (new Type[] {XmlDoc.GetType()}) ;
     System.Messaging.Message msg = mq.Receive() ;
         //XmlDoc =(System.Xml.XmlDocument)msg.Body;
         //  XmlDoc.Save("path to save");
         // MessageBox.show (XmlDoc.ToString());

    byte[] buffer = new byte[msg.BodyStream.Length];
      string sXml = System.Text.Encoding.ASCII.GetString(buffer);  // if you need the xml string
     FileStream fs = File.Create("path to save");


UmamahezAuthor Commented:
Hello Sunmix,
Thank you very much for your replies. I already implemented body stream. the xml formatter was failing bacause the XMl file had some comma separated values attached to it in the beginning. I removed those and then loaded teh string onto a xmldoc and saved the doc.

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now