Bluetears76,
I noticed a few things in the code you posted:
* You don't use the same type when you instantiate the XmlSerializer to
serialize and deserialize the message. Both of them should probably look
like:
XmlSerializer = new XmlSerializer( typeof( Message ) );
Maybe it should be typeof( MessageContaine r ), but I can't tell without
knowing more.
To make sure that XmlSerializer can process FileMessage and ChatMessage, you
need to decorate the MessageClass with XmlInclude attributes like this:
[XmlInclude( typeof( FileMessage ) )]
[XmlInclude( typeof( ChatMessage ) )]
public class Message{
// ...
}
Alternatively you might be able to do:
public class MessageContaine r
{
[XmlElement(type of(FileMessage) )]
[XmlElement(type of(ChatMessage) )]
public Message content;
// other stuff ...
}
* You are instantiating and XmlSerializer for every serialization and
deserialization operation. It's much more efficient to keep the
XmlSerializer around and re-use it.
* You are using ref in your method signatures. Are you going across
AppDomains with these methods? Is there any reason to do that? Crossing
AppDomain boundaries is a very expensive operation. I don't see a reason why
you need to do this here.
* Are you really building a loosely coupled app or is this in a tightly
coupled scenario where the BinaryFormatter might be the better choice. The
decisive criteria here would be: a) do you really need to transmit XML? b)
do both endpoint of the transmission always have the same assemblies with
the serialized types available? If the answer to a) is no and b) is yes then
the BinaryFormatter is the better choice.
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor
"Bluetears7 6" <bl*********@ya hoo.com> wrote in message
news:3c******** *************** ***@posting.goo gle.com...
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=n ull;
try
{
XmlSerializer serializer=new XmlSerializer(m essage.GetType( ));
netWorkStream=n ew NetworkStream(_ clientSocket);
Stream stream=(Stream) netWorkStream;
serializer.Seri alize(stream,me ssage);
}
finally
{
netWorkStream.C lose();
}
}
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 DeserializeMess age(ref NetworkStream networkStream,r ef
Message message)
{
XmlSerializer deserializer=nu ll;
bool result=false;
int count=0;
Byte []buffer=new Byte[BUFFER_SIZE];
deserializer=ne w XmlSerializer(t ypeof(MessageCo ntainer));
count=networkSt ream.Read(buffe r,0,buffer.Leng th);
if(count <= 0)
{
message=null;
return false;
}
MemoryStream memoryStream=nu ll;
memoryStream=ne w MemoryStream(bu ffer,0,count);
message= ((MessageContai ner)deserialize r.Deserialize(m emoryStream));
//Get An Exception here as the type is ChatMessage and i am expecting
a message
}