Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

XmlSerializer over network stream using Inherited Objects

Posted on 2003-11-14
2
Medium Priority
?
832 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 1200 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 800 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month6 days, 8 hours left to enroll

783 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