Introduction
This article is for setting and retrieving object from Microsoft Messaging Queue.
Background
This is console application which is used to read a xml file and deserialized it into an object .This is defined in xmlMappingClass.cs file in this project.Finally ,this object is then sent to MMQ which can be fetched later.
Using the code
I have defined three classes here which are sufficient to send and retrieve an object.
1.run.cs: This class is starting point of this application which create a queue and then insert object of xml file(kept in data Folder).Here,we can also find the code to retrive an object from queue and is then printed on the console.
string queuePath = ".\\PRIVATE$\\Questions";
MSQUtility.SendXmlObjectToQueue(Environment.CurrentDirectory+"\\Data\\Questions.xml", queuePath);
string sXML = MSQUtility.ReadXmlObjectFromQueue(queuePath);
Console.Write(sXML);
MSQUtility.ClearQueue(queuePath);
2.MSQUtility.cs: In this class,all the functions for reading and sending an object into queue have been written.
public static void SendXmlObjectToQueue(string xmlFilePath, string queuePath)
{
EnsureQueueExists(queuePath);
MessageQueue queue = new MessageQueue(queuePath);
object GetallQuestionsFromXmlFile = DeserializeObject(xmlFilePath, (typeof(Questions)));
Questions allQuestions = (Questions)GetallQuestionsFromXmlFile;
queue.Send(allQuestions);
}
public static void EnsureQueueExists(string path)
{
if (!MessageQueue.Exists(path))
{
MessageQueue.Create(path);
}
}
public static string ReadXmlObjectFromQueue(string sQueue)
{
MessageQueue oQueue;
Message oMessage;
try
{
if (!MessageQueue.Exists(sQueue)) { MessageQueue.Create(sQueue); }
oQueue = new MessageQueue(sQueue);
oQueue.Formatter = new CustomMessageFormatter();
oMessage = oQueue.Receive(new TimeSpan(0, 0, 5));
string sXML = (string)oMessage.Body;
try { oQueue.Close(); }
catch (Exception) { }
oQueue.Close();
return sXML;
}
catch (Exception e) { Console.WriteLine(e.Message); }
return null;
}
public static void ClearQueue(string sQueue)
{
MessageQueue oQueue;
try
{
if (!MessageQueue.Exists(sQueue)) { MessageQueue.Create(sQueue); }
oQueue = new MessageQueue(sQueue);
oQueue.Purge();
oQueue.Close();
}
catch (Exception e) { Console.WriteLine(e.Message); }
}
public static Object DeserializeObject(string xmlFilePath, Type objectType)
{
XmlSerializer xs = new XmlSerializer(objectType);
FileStream fs = new FileStream(xmlFilePath, FileMode.Open, FileAccess.Read);
object obj = xs.Deserialize(fs);
fs.Close();
return obj;
}
3.XmlMappingClass.cs: This class is mapping class for the Xml file which holds serialized/Deserialized object.
[Serializable()]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public partial class Questions{
public Questions(){
}
private Question[] m_question;
private string m_type;
private string m_language;
[XmlElement("Question", Form = XmlSchemaForm.Unqualified)]
public Question[] Question
{
get { return m_question; }
set { m_question = value; }
}
[XmlAttribute()]
public string Type
{
get { return m_type; }
set { m_type = value; }
}
[XmlAttribute()]
public string Language
{
get { return m_language; }
set { m_language = value; }
}
}
[Serializable()]
[XmlType(AnonymousType = true)]
public partial class Question
{
public Question()
{
}
private string m_id;
private string m_subType;
private string m_toughLevel;
private string m_ques;
[XmlAttribute()]
public string Id
{
get { return m_id; }
set { m_id = value; }
}
[XmlAttribute()]
public string SubType
{
get { return m_subType; }
set { m_subType = value; }
}
[XmlAttribute()]
public string ToughLevel
{
get { return m_toughLevel; }
set { m_toughLevel = value; }
}
[XmlAttribute()]
public string Ques
{
get { return m_ques; }
set { m_ques = value; }
}
}
Probably,you may have liked this article...i have tried to make such a simple console application which can be understood even by a beginnner.
References:
http://www.eggheadcafe.com/articles/20021221.asp
http://msdn2.microsoft.com/en-us/library/4dhdwx8w(VS.71).aspx
http://msdn2.microsoft.com/EN-US/library/wss66xs0(VS.71).aspx
http://www.developerfusion.co.uk/show/2131/2/