Link to home
Start Free TrialLog in
Avatar of LOXjet
LOXjetFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

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

Avatar of LOXjet

ASKER

Hi kaufmed,

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

Regards
LJ
Is it possible to display the actual class definitions (or a workable abbreviation of such) to see how/what you are doing?
Avatar of LOXjet

ASKER

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".
Avatar of Mike_Mozhaev
Mike_Mozhaev

Avatar of LOXjet

ASKER

Thanks Mike, unfortunately that relates to Win Forms only. Shame as it's a handy feature.
ASKER CERTIFIED SOLUTION
Avatar of LOXjet
LOXjet
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.