Solved

XmlSerializer over network stream using Inherited Objects

Posted on 2003-11-14
2
825 Views
Last Modified: 2008-10-07
Hi I have a hirachy of classes which are Message(base), then FileMessage and ChatMessage (extended)

I want to serialize the objects and when i am deserizaling i dont know if i am getting FileMessage or ChatMessage. So how to get that object and use it
I have written following code for serialization
public void Send(Message message)
{
NetworkStream netWorkStream=null;
      try
      {
      XmlSerializer serializer=new XmlSerializer(message.GetType());
      netWorkStream=new NetworkStream(_clientSocket);
      Stream stream=(Stream)netWorkStream;
      serializer.Serialize(stream,message);
      }
      finally
      {
            netWorkStream.Close();
      }
}

It send the message fine.
but when i deseralize the message i dont know which type of message it is .. i,e, a FileMessage or a ChatMessage so i get an exception

following is the code of deserlizeation
public bool DeserializeMessage(ref NetworkStream networkStream,ref Message message)
{
      XmlSerializer deserializer=null;
      bool result=false;
      int count=0;
      Byte []buffer=new Byte[BUFFER_SIZE];
      deserializer=new XmlSerializer(typeof(MessageContainer));
      count=networkStream.Read(buffer,0,buffer.Length);
      if(count <= 0)
      {
            message=null;
            return false;
      }
      MemoryStream memoryStream=null;
      memoryStream=new MemoryStream(buffer,0,count);
      message= ((MessageContainer)deserializer.Deserialize(memoryStream)); //Get An Exception here as the type is ChatMessage and i am expecting a message
}
0
Comment
Question by:armoghan
2 Comments
 
LVL 6

Accepted Solution

by:
DaniPro earned 300 total points
ID: 9747023
You can try to solve with the exception

try
{
     message= ((MessageContainer)deserializer.Deserialize(memoryStream));
}
catch (Exception exp) // ... better if you specify the Exception type
{
      message= ((ChatMessage)deserializer.Deserialize(memoryStream));
}


0
 
LVL 10

Assisted Solution

by:ptmcomp
ptmcomp earned 200 total points
ID: 9749195
How about:

you change:
message= ((MessageContainer)deserializer.Deserialize(memoryStream)); //Get An Exception here as the type is ChatMessage and i am expecting a message

to:

object obj = deserializer.Deserialize(memoryStream));
if (obj is MessageContainer)
{
    message= (MessageContainer)obj
}
else
{
    chatMessage = (ChatMessage)obj
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 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

14 Experts available now in Live!

Get 1:1 Help Now