Solved

XmlSerializer over network stream using Inherited Objects

Posted on 2003-11-14
2
827 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

685 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