Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Xml deserialization error

Started by Kaze Sep 30, 2008 at 2:13 AM 2 replies 1.6k views
Original Post
Kaze
Kaze
As far as I can tell when serializing a number of elements if the list is empty their is no reference to the element name in the output. The problem is that deserializing the non existent list throws a exception so now I have a situation where it serialize fine but can't deserialize. Am I doing something wrong or is their a reason why this happens?

private List<AbilityData> abilities = new List<AbilityData>();
        [XmlElement("ability")]
        public AbilityData[] Abilities
        {
            get { return abilities.ToArray(); }
            set
            {
                abilities = new List<AbilityData>();
                abilities.AddRange(value);
            }
        }
        public void AddAbility(AbilityData ability)
        {
            abilities.Add(ability);
        }



VizOne
VizOne
Hi,

Next time try to describe your problem better. For example, which exception was thrown? What was the stack trace at that point? If you had a look at the stack trace you'd probably realized that you're getting an ArgumentNullException somewhere in abilities.AddRange. The serializer assigns null to the Abilities property during deserialization if the list is empty. To handle this change your setter to something like:
set {  abilities = new List<AbilityData>();  if (value != null) {    abilities.AddRange(value);  }}

Deserialization should run fine now.

Regards,
Andre
Kaze
Kaze
Thanks, that worked

Though the exception I was getting, "There is an error in XML document (7, 3)." could have been more specific.

It's not giving me the option to trace the stack, unless I'm missing some option I imagine its because the deserialization is taking place in another assembly.
VizOne
VizOne
You could always try to use the debugger, which will give you a lot information in case of an exception.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.