Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 7th, 2006, 01:55 PM
Thyme
Guest
 
Posts: n/a
Default 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
  #2  
Old August 8th, 2006, 06:35 AM
Keith Patrick
Guest
 
Posts: n/a
Default Re: Serializing multiple objects in one XML document

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" <Thyme@discussions.microsoft.comwrote in message
news:7CD339E5-27DC-41D4-B27A-8E790BE787F2@microsoft.com...
Quote:
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

  #3  
Old August 8th, 2006, 01:45 PM
Thyme
Guest
 
Posts: n/a
Default Re: Serializing multiple objects in one XML document

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:
Quote:
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" <Thyme@discussions.microsoft.comwrote in message
news:7CD339E5-27DC-41D4-B27A-8E790BE787F2@microsoft.com...
Quote:
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
>
>
>
  #4  
Old August 8th, 2006, 07:45 PM
Keith Patrick
Guest
 
Posts: n/a
Default Re: Serializing multiple objects in one XML document

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.


 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles