Monday, July 13, 2009

Read MSMQ through C# Application

How to receive an XML from MSMQ:

Consider that we have a private queue named “Biztalk” and we receive an xml message in that queue. Now to read this xml in our .net application we need to follow the following code snippets :

Step 1: Create a C# Application
Step 2: refer System.Messaging.dll

Step 3: Copy the following code snippet and paste in the place you require

[ Code Snippet ]

// General Queue Path Format : FORMATNAME:DIRECT=OS:[SystemName]\PRIVATE$\[QueueName]
System.Messaging.MessageQueue MyQueue = new System.Messaging.MessageQueue([QueuePath]);
//Since we are receiving xmldocument we have to specify type as XMLDocument
MyQueue.Formatter =
new System.Messaging.XmlMessageFormatter(new Type[] { typeof(XmlDocument) });
System.Messaging.Message MyMsg = MyQueue.Receive;
//Now convert the message to our requirement
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc = (XmlDocument) MyMsg.Body;
xmlDoc.Save(@"d:\[filename].xml");

[/ Code Snippet ]

Step 4: Run the application and find the resulting xml file in the place you have specified.

No comments:

Post a Comment