473,396 Members | 1,996 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,396 software developers and data experts.

Serializing multiple objects in one XML document

Each time I serialize an object using XmlSerializer I get a structure like
this:
<?xml version="1.0"?>
<MyType>
..
..
</MyType>
<?xml version="1.0"?>
<MyType>
..
..
</MyType>

Instead I would like

<?xml version="1.0"?>
<MyRootThatIWillWriteToTheStream>
<MyType>
..
..
</MyType>
<MyType>
..
..
</MyType>
</MyRootThatIWillWriteToTheStream>

So in other words, can I avoid writing the XML 1.0 decoration header each
time?

--
Thanks
Aug 7 '06 #1
3 2783
An XML document can only have 1 root, so you have to add the objects to a
container, such as a List. It'll serialize out to <ArrayOfAnyTypeunless
you use a type-specific collection, though. Since all your objects are of
the same type, it'll serialize to ArrayOfMyType

"Thyme" <Th***@discussions.microsoft.comwrote in message
news:7C**********************************@microsof t.com...
Each time I serialize an object using XmlSerializer I get a structure like
this:
<?xml version="1.0"?>
<MyType>
.
.
</MyType>
<?xml version="1.0"?>
<MyType>
.
.
</MyType>

Instead I would like

<?xml version="1.0"?>
<MyRootThatIWillWriteToTheStream>
<MyType>
.
.
</MyType>
<MyType>
.
.
</MyType>
</MyRootThatIWillWriteToTheStream>

So in other words, can I avoid writing the XML 1.0 decoration header each
time?

--
Thanks

Aug 8 '06 #2
Thanks. I have object of different types. I have tried with Object[] and
ArrayList but when I serialize I get an exception:

Unhandled Exception: System.InvalidOperationException: There was an error
generating the XML document. ---System.InvalidOperationException: The type
Company.System.Name.SchemaType was not expected. Use the XmlInclude or
SoapInclude attribute to specify types that are not known statically.
--
Thanks
"Keith Patrick" wrote:
An XML document can only have 1 root, so you have to add the objects to a
container, such as a List. It'll serialize out to <ArrayOfAnyTypeunless
you use a type-specific collection, though. Since all your objects are of
the same type, it'll serialize to ArrayOfMyType

"Thyme" <Th***@discussions.microsoft.comwrote in message
news:7C**********************************@microsof t.com...
Each time I serialize an object using XmlSerializer I get a structure like
this:
<?xml version="1.0"?>
<MyType>
.
.
</MyType>
<?xml version="1.0"?>
<MyType>
.
.
</MyType>

Instead I would like

<?xml version="1.0"?>
<MyRootThatIWillWriteToTheStream>
<MyType>
.
.
</MyType>
<MyType>
.
.
</MyType>
</MyRootThatIWillWriteToTheStream>

So in other words, can I avoid writing the XML 1.0 decoration header each
time?

--
Thanks


Aug 8 '06 #3
Have you tried mapping an XML namespace to that particular type via the
XmlSerializer(XmlNameTable) constructor? Also, you could just mark the
class with [XmlInclude(typeof(Company.System.Name.SchemaType))] to help the
serializer a bit with knowing how to serialize the type.
You may also run into an issue if your classes implement interfaces, as I
recall having issues trying to deserialize Winform objects, and the
interfaces kept biting me. I ditched XmlSerializer altogether for my
configuration API due to that limitation, going with a sorta home-rolled
XAML instead (although XAML wasn't suitable as a serialization format
because it resides in the WPF assemblies rather than a more general purpose
one, whereas I'm using config serialization in a non-UI library.) Worst
comes to worst, you can always go that route as long as your custom
serializer is the only one (de)serializing.
Aug 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tom Kent | last post by:
I have an array list of objects that I want to serialize into an XML file. I have tried this using the following code, but it gives me an error message An unhandled exception of type...
1
by: Ivo Bronsveld | last post by:
All, I have quite a challenging task ahead of me. I need to write an object model (for code access) based on a schema, which cannot be made into a dataset because of it's complexity. So I...
3
by: Bruno Jouhier | last post by:
Is there a way to serialize a graph of objects and get the output as an XML document, without first serializing to text and then parsing the text (I know how to do this but I find it really silly...
2
by: Tobias Zimmergren | last post by:
Hi, just wondering what serializing really is, and howto use it? Thanks. Tobias __________________________________________________________________ Tobias ICQ#: 55986339 Current ICQ status: +...
4
by: Dave Veeneman | last post by:
When does serializing objects make more sense than persisting them to a database? I'm new to object serialization, and I'm trying to get a feel for when to use it. Here is an example: I'm...
0
by: JackRazz | last post by:
I'm trying to serialize a collection to a file stream by serializing each object individually. The code below works fine with the BinaryFormatter, but the SoapFormatter reads the first object and...
3
by: axr | last post by:
Having trouble with Serilization of objects that contain members which are of type Interface eg public class SomeClass { ISomeInterface1 itf1; ClassType1 ct1; ISomeInterface2 itf2;
47
by: Max | last post by:
Due to the behaviour of a particular COM object, I need to ensure that a request for a particular ASP page is finalized before another request for the page is processed. Does IIS have a way to...
1
by: falcon198198 | last post by:
Greetings: I was hoping for some advice on an application. Here is what I am working on: Basically I have a bigger application that is having a printing problem making multiple copies of a pdf...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.