Solved

XmlSerializer over network stream using Inherited Objects

Posted on 2003-11-14
2
824 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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

9 Experts available now in Live!

Get 1:1 Help Now