Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - How to Deserialise an XML stream Polymorphically

Reply
 
Old 03-12-2009   #1 (permalink)
Charles


 
 

How to Deserialise an XML stream Polymorphically

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



My System SpecsSystem Spec
Old 03-12-2009   #2 (permalink)
Martin Honnen


 
 

Re: How to Deserialise an XML stream Polymorphically

Charles wrote:
Quote:

> 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.



--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
My System SpecsSystem Spec
Old 03-12-2009   #3 (permalink)
Charles


 
 

Re: How to Deserialise an XML stream Polymorphically

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


"Martin Honnen" <mahotrash@xxxxxx> wrote in message
news:OhjX$3zoJHA.3876@xxxxxx
Quote:

> Charles wrote:
>
Quote:

>> 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.
>
>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/

My System SpecsSystem Spec
Old 03-12-2009   #4 (permalink)
Martin Honnen


 
 

Re: How to Deserialise an XML stream Polymorphically

Charles wrote:
Quote:

> 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.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
My System SpecsSystem Spec
Old 03-12-2009   #5 (permalink)
Charles


 
 

Re: How to Deserialise an XML stream Polymorphically

That makes sense. Many thanks again.

Charles


"Martin Honnen" <mahotrash@xxxxxx> wrote in message
news:%23$1lVG0oJHA.3876@xxxxxx
Quote:

> Charles wrote:
>
Quote:

>> 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.
>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
WMV stream seeking? .NET General
.wmv will not stream on wmp Vista General
Stream Live TV Vista General
How to stream video Vista music pictures video
Stream recording Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46