Clamport
Clamport

Reputation: 23

Deserializing .NET stream with multiple objects

I have a MemoryStream which I write into as I receive data off the network. Since the data can be broken up, there is the potential for the stream to have a partial message or multiple messages stored in the stream. When deserializing, I place the pointer back at the beginning of the stream and try to deserialize a class of mine. I have the deserialize wrapped in a try catch block, but I get to the deserialize line, the application just quits out (no exception, not more lines run in the function, etc).

I have multiple questions:

  1. What is the best way to receive a stream of XML data from the network that may or may not be complete, and if so may or may not have more than one message?
  2. Does the deserializer need to know about the encoding to decode the XML within the MemoryStream?
  3. Does deserialization place the stream pointer after the deserialized object?
  4. Can you deserialize multiple objects within a single stream?

Upvotes: 1

Views: 781

Answers (1)

Domin8urMind
Domin8urMind

Reputation: 1314

1) You can leverage the XmlReader class which "provides forward-only, read-only access to a stream of XML data". That may help you translate xml data that may not be complete. http://msdn.microsoft.com/en-us/library/vstudio/system.xml.xmlreader

2) If you are referring to the mixing ASCII, UTF-8, etc. then yes, otherwise I am not sure what the question is.

3) That depends on the deserializer you are using.

4) Yes, with the XMlReader class you can cleverly extract attributes and xml fragments for later consumption (although the solution is not elegant and rather ugly)

Upvotes: 0

Related Questions