Deserialize Empty\Null Collection

Hello Everyone,

I’m trying to deserialize a collection (let’s use order\items for simplicity’s sake) and I'm struggling to get the serializer to differentiate between an empty collection (where the element is specified but not populated) and a NULL collection (where the element isn't specified)

These are my serialization classes :-

MyOrderItems

public class MyOrderItems : Collection<MyOrderItem>

MyOrder

public class MyOrder

[XmlArray("items")]
public MyOrderItems Items = null;

XmlSerializer.Deserialize turns this into an empty collection :-

<order>
  <items>
  </items>
</order>

But also this :-

<order>
</order>

What I need is for the second example to leave "Items" as NULL.

Any suggestion on the simplest\best way of telling the serializer to ignore collections that aren't specified?

I hope that makes sense!

Regards
LJ
LOXjetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
I would have to see some of your code to make a better attempt at this, but in the meantime, have you tried setting the "IsNullable" property in your XmlArray member?
[XmlArray("items", IsNullable = true)]
public MyOrderItem items = null;

Open in new window

0
LOXjetAuthor Commented:
Hi kaufmed,

Yes I have, sorry I should have mentioned that. It made no difference unfortunately.

Regards
LJ
0
käµfm³d 👽Commented:
Is it possible to display the actual class definitions (or a workable abbreviation of such) to see how/what you are doing?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

LOXjetAuthor Commented:
That's pretty much it for the serialization classes (that's relevant to the problem anyway).

The only thing that's probably worth mentioning is that deserilization happens as part of a custom config section (the XML to deserialize is buried inside a config file). So in the config classes I use a custom "DeserializeSection"...

  public class MyConfigSection : ConfigurationSection
  {

    public MyConfig Config;

    protected override void DeserializeSection(XmlReader reader)
    {
      // Initialise...
      XmlDocument xmlDocument = new XmlDocument();
      MyXmlSerialiser xmlSerialiser = new MyXmlSerialiser();
      // Move to element [system]...
      reader.ReadStartElement();
      // Load xml...
      xmlDocument.Load(reader);
      // Deserialise...
      xmlSerialiser.Deserialise<MyConfig>(xmlDocument, out Config);
    }

"MyXmlSerialiser" is our own XML serialization class, but ultimately it just uses the standard "XmlSerializer" and "XmlNodeReader".
0
Mike_MozhaevCommented:
0
LOXjetAuthor Commented:
Thanks Mike, unfortunately that relates to Win Forms only. Shame as it's a handy feature.
0
LOXjetAuthor Commented:
I've managed to find a workaround and it's an acceptable one, for the time being.

http://www.codemeit.com/code-collection/xml-deserialization-xml-undefined-property-becomes-an-empty-list.html

Basically, stop using a custom list class...

public class MyOrderItems : Collection<MyOrderItem>

[XmlArray("items")]
public MyOrderItems Items = null;

And use an object array...

[XmlArray("items")]
public MyOrderItem[] Items = null;

The upside is no interface or XML changes. The downside is the 'list' elements cannot have attributes (not an issue for me at the moment).

Thanks for the help everyone.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tom KnowltonWeb developerCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.