• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2843
  • Last Modified:

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
0
LOXjet
Asked:
LOXjet
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now