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();
xmlDoc.Load("C:\\Test\\test.xml");
mq.send(xmlDoc)


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

            
UmamahezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sumixCommented:

 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:
          [Serializable]
          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"?>
<Customer>
  <CustomerID>5</CustomerID>
  <CustomerName>ABC</CustomerName>
</Customer>

  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;
      MessageBox.Show(c2.CustomerName);

   ......
 
 
0
sumixCommented:

   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];
    msg.BodyStream.Read(buffer,0,(int)msg.BodyStream.Length);
      string sXml = System.Text.Encoding.ASCII.GetString(buffer);  // if you need the xml string
     FileStream fs = File.Create("path to save");
     fs.Write(buffer,0,buffer.Length);
     fs.Close();

 sumix

   
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.