MSMQ and XML document

Posted on 2006-04-03
Last Modified: 2008-01-09
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");
Many Thanks

Question by:Umamahez
    LVL 12

    Expert Comment


     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;

    LVL 12

    Accepted Solution


       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");
             // (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");



    Author Comment

    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

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now