Solved

XmlSerializer over network stream using Inherited Objects

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

825 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