How to avoid XmlSerializer from creating empty collections

D

Daniel

Hi,
Lets say I have the following XML:

<Person>
<Name/>
<Age/>
</Person>

Now, in addition to name and age, the class that loads this xml has a
public data member named Children of type ChildrenCollection which
implements ICollection.

The problem is that XmlSerializer notice that the Children data member
implements the collection interface and creates an empty collection
even if the children section does not exists in the incoming xml. I
would rather have a null pointer in this case for several reasons.

Is there a way to tell the XmlSerializer not to create the empty
collection?

Thanks,
Daniel
 
D

Daniel

Hi,
Lets say I have the following XML:

<Person>
<Name/>
<Age/>
</Person>

Now, in addition to name and age, the class that loads this xml has a
public data member named Children of type ChildrenCollection which
implements ICollection.

The problem is that XmlSerializer notice that the Children data member
implements the collection interface and creates an empty collection
even if the children section does not exists in the incoming xml. I
would rather have a null pointer in this case for several reasons.

Is there a way to tell the XmlSerializer not to create the empty
collection?

Thanks,
Daniel


I've tried using the "Specified" flag (which by the way I could not
find any documentation of) with no lock.
The XmlSerializer keeps throwing the weird exception of the "assembly
with random name not found". Apparently it does not allow collection
objects to have the "specified" flag.

Is this behavior documented somewhere?
 
J

Jason Sobell

Why not simply make Children a property, and if it's an empty collection
then return the value Null?
Beware of using XMLSerialization to try and structure specific complex
structures for your application. It's excellent for serializing and
deserializing object hierarchies in its own format, but can be a sod if you
want to try and force your own formatting rules.
To completely prevent optional tags being generated you will probably have
to implement your own serialization methods instead of the .NET ones.
This is a bit annoying and seems wasteful, but it allows you to customize
all aspects of the output, and allows things like complex tags to be output
more easily.
If you only want to override the XML handling for one or two objects,
consider providing overloaded methods from the IXmlSerializable interface.
I use this to read and write complex embedded tags, such as formatted text
such as "This <B>is</B> an <I>italic and <U>underlines</U></I> string",
which you can't write out in XML format using normal serialization as the
serializer will (quite correctly) escape the contents.

Cheers,
Jason
 
D

Daniel

Thanks for replying Jason,
Adding the Children as an element is what I did eventually, but I
still would like to see this technical issue solved in a more
technical manner. Why should I change my schema just because MS missed
a spot?

Overriding the IXmlSerializable is an option I've looked at, but I've
read too many warnings in the MSDN about it being subject to changes
in the future. I would simply not be able to sleep at night anymore...
:)

If there is a way to make the "Specified" flag work, it would be the
best.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top