How to Deserialise an XML stream Polymorphically

  • Thread starter Thread starter Charles
  • Start date Start date
C

Charles

I am writing and reading XML on a TCP connection. Once I have sent an XML
message I have to wait for a reply, which could be in one of several forms.
All the possible replys have a basic set of attributes in common, but then
might have additional information.

I have a base class for the common attributes and derived classes for the
variants.

What I would like to do is deserialise the reply, polymorphically, to an
object of the derived class but I'm not sure how to do that. I have tried
deserialising as a generic reply in order to test what type of object I
have, but the deserialise call fails with an error in the stream. I can
understand why it fails, because there are attributes in the stream that do
not exist in the base class, but that doesn't get me any closer to the
solution.

If I know what derived class the reply is I can explicitly deserialise as
that object type, but in normal operation I don't know what object type I am
receiveing until I have deserialised it, if that makes sense.

Does anyone have an idea how this might be done?

TIA

Charles
 
Charles said:
If I know what derived class the reply is I can explicitly deserialise as
that object type, but in normal operation I don't know what object type I am
receiveing until I have deserialised it, if that makes sense.

Does anyone have an idea how this might be done?

Load into an XmlDocument first, then you can check the root element
(e.g. LocalName, NamespaceURI, attributes) to hopefully be able to tell
what kind of derived type you are dealing with, then you can feed an
XmlNodeReader over the XmlDocument (or its DocumentElement) to
XmlSerializer.
 
Hi Martin

Thanks for the very quick reply. That sounds like just the sort of thing I
was after.

Out of interest, suppose I try to deserialise the network stream associated
with my TCP connection before the full XML reply has come in, will I get an
obvious error? Is there a standard way of ensuring that I have a complete
XML message, or a proper way to detect that I haven't?

Charles
 
Charles said:
Out of interest, suppose I try to deserialise the network stream associated
with my TCP connection before the full XML reply has come in, will I get an
obvious error? Is there a standard way of ensuring that I have a complete
XML message, or a proper way to detect that I haven't?

If you parse XML with XmlDocument then it expects a well-formed document
as defined in the XML specification, so you need to have a single root
element containing all other elements, otherwise you will get an
XmlException.

So consuming the network stream directly with the Load method of an
XmlDocument is probably not going to work if you don't know for sure the
XML is complete, you will to buffer the data.
 
That makes sense. Many thanks again.

Charles


Martin Honnen said:
If you parse XML with XmlDocument then it expects a well-formed document
as defined in the XML specification, so you need to have a single root
element containing all other elements, otherwise you will get an
XmlException.

So consuming the network stream directly with the Load method of an
XmlDocument is probably not going to work if you don't know for sure the
XML is complete, you will to buffer the data.
 
Back
Top