469,640 Members | 1,563 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,640 developers. It's quick & easy.

XML Serialize/Deserialize multiple objects from a single XML file.

After doing a search through google's archives of this list, I didn't see
what I was looking for so here goes...

Is it possible to serialize/deserialize multiple objects from a single XML
file? I was looking to use the XML serialization to be able to translate
complex XML elements in a single XML file through XML serialization as
opposed to using SAX and coding my own serializer. However, the XML
Serialization API appears to only allows for reading/writing each object as
its own XML document via the XmlSerializer.Serialize method. Granted, it
would be possible to use a MemoryStream as the output/input and strip out
the offending bits after each object deserializes itself, but that seems to
be kludgy method of serialization. Deserialization is nearly the same as
you'd have to get each Xml element that represent the object to be
deserialized as its own stream first before calling the Serialize method.

Is this correct or did I miss something somewhere?
Nov 12 '05 #1
2 11393
It's one object graph per document, if you are using an Xml file.
For example if your class contains multiple public members, all of different
type, all the public members will be serialized (modulo attributes on the
members).

As you said, you can serialize multiple independent objects to/from a
stream, if you so desire.

-Dino

--
Dino Chiesa
Microsoft Developer Division
d i n o c h @ o n l i n e . m i c r o s o f t . c o m

"Hollywood" <ho*******@thzero.com> wrote in message
news:OL**************@TK2MSFTNGP11.phx.gbl...
After doing a search through google's archives of this list, I didn't see
what I was looking for so here goes...

Is it possible to serialize/deserialize multiple objects from a single XML
file? I was looking to use the XML serialization to be able to translate
complex XML elements in a single XML file through XML serialization as
opposed to using SAX and coding my own serializer. However, the XML
Serialization API appears to only allows for reading/writing each object as its own XML document via the XmlSerializer.Serialize method. Granted, it
would be possible to use a MemoryStream as the output/input and strip out
the offending bits after each object deserializes itself, but that seems to be kludgy method of serialization. Deserialization is nearly the same as
you'd have to get each Xml element that represent the object to be
deserialized as its own stream first before calling the Serialize method.

Is this correct or did I miss something somewhere?

Nov 12 '05 #2
Your problem is not the XmlSerializer, it's the XmlTextWriter that enforces
the creation of well formed Xml documents.

You could serialize multiple object graphs to the same stream as long as you
switched out the XmlTextWriter instance if it wasn't for some weird behavior
of the XmlSerializer that always writes out a document declaration when it
sees a new XmlTextWriter ...

You can work around that issue using my XmlFragmentWriter sample on GDN [0]
..

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

[0]
http://www.gotdotnet.com/Community/U...b-3a0a9e13bbe3

"Hollywood" <ho*******@thzero.com> wrote in message
news:OL**************@TK2MSFTNGP11.phx.gbl...
After doing a search through google's archives of this list, I didn't see
what I was looking for so here goes...

Is it possible to serialize/deserialize multiple objects from a single XML
file? I was looking to use the XML serialization to be able to translate
complex XML elements in a single XML file through XML serialization as
opposed to using SAX and coding my own serializer. However, the XML
Serialization API appears to only allows for reading/writing each object as its own XML document via the XmlSerializer.Serialize method. Granted, it
would be possible to use a MemoryStream as the output/input and strip out
the offending bits after each object deserializes itself, but that seems to be kludgy method of serialization. Deserialization is nearly the same as
you'd have to get each Xml element that represent the object to be
deserialized as its own stream first before calling the Serialize method.

Is this correct or did I miss something somewhere?

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Colin Basterfield | last post: by
14 posts views Thread by vince | last post: by
5 posts views Thread by David Sworder | last post: by
2 posts views Thread by films | last post: by
reply views Thread by John Manion via .NET 247 | last post: by
4 posts views Thread by =?Utf-8?B?Qnlyb24=?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.