LOXjet
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
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
ASKER
Hi kaufmed,
Yes I have, sorry I should have mentioned that. It made no difference unfortunately.
Regards
LJ
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?
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(XmlRead er 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>( xmlDocumen t, out Config);
}
"MyXmlSerialiser" is our own XML serialization class, but ultimately it just uses the standard "XmlSerializer" and "XmlNodeReader".
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(XmlRead
{
// Initialise...
XmlDocument xmlDocument = new XmlDocument();
MyXmlSerialiser xmlSerialiser = new MyXmlSerialiser();
// Move to element [system]...
reader.ReadStartElement();
// Load xml...
xmlDocument.Load(reader);
// Deserialise...
xmlSerialiser.Deserialise<
}
"MyXmlSerialiser" is our own XML serialization class, but ultimately it just uses the standard "XmlSerializer" and "XmlNodeReader".
ASKER
Thanks Mike, unfortunately that relates to Win Forms only. Shame as it's a handy feature.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Open in new window