473,395 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 11625
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ian Tompsett | last post by:
H I was wondering if it possible for an object to serialize/deserialize itself from XML. I'd be guessing that it would need to use the XmlSerializer class, but that seems to want to create a...
2
by: Colin Basterfield | last post by:
Hi, I have a list which is derived from CollectionBase, and it contains a list of User objects, which I want to Serialize out to an XML file. Is there anywhere where I can find how to decode...
14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
2
by: films | last post by:
I understand the concept. Serialization of a class will add all the sub-objects of the class to the stream if there are also serializible. So say I have: class Author {
3
by: Gislain | last post by:
Hello, I'm looking for a best way to serialize multiple / different object in the same XML file, and of course, how can i deserialize it. Actualy, when i serialize the first object in XML...
5
by: Brad | last post by:
I would like to serialize an arraylist of objects to xml so I can store the xml in a database column. How would I code the serializing and deserializing? Below is a (overly) simple, incomplete...
0
by: John Manion via .NET 247 | last post by:
Long Post, thanks for your patience... I have and XML file that looks something like this: <?xml version="1.0" encoding="utf-8" ?> <Settings> <Location> <X>30</X> <Y>40</Y> </Location>...
4
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.