XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

XML List Serialization

Last post 2/27/2009 7:35 PM by Tigernado. 4 replies.
  • 2/27/2009 8:22 AM

    XML List Serialization


    I'm designing a behavior tree system for the AI of our game. There's a Behavior base class and several derived Behaviors. Each behavior has its own children defined as a generic list (List<Behavior>). I want to do something with generic lists that appears to be limited to ArrayLists  from looking at tons of documentation and tutorials. I want to serialize/deserialize behavior trees with xml, but each behavior has its own unique properties that should be included in the xml file. With an ArrayList I think it would be something like this:

    namespace BehaviorTrees 
        public class Behavior 
        { 
            [XmlArray("Children")] 
            [XmlArrayItem("Behavior"typeof(Behavior))] 
            [XmlArrayItem("DerivedBehavior",typeof(DerivedBehavior))] 
            public ArrayList Children; 
        } 
     
        public class DerivedBehavior : Behavior 
        { 
            public int NewProperty { getset; } 
        } 


    But how do I do this with a List<T>?





    Leave luck to heaven.
  • 2/27/2009 5:45 PM In reply to

    Re: XML List Serialization

    Replace ArrayList with List<Behavior> (or List<object> if you really want the exact same loosely typed semantics as ArrayList).

    Since generics were added in the 2.0 CLR, there is never a good reason to use ArrayList any more. It is only kept around for backward compatibility with older code, and should not be used when writing new code.
    XNA Framework Developer - blog - homepage
  • 2/27/2009 6:14 PM In reply to

    Re: XML List Serialization

    Shawn Hargreaves:
    Replace ArrayList with List<Behavior> (or List<object> if you really want the exact same loosely typed semantics as ArrayList).

    Since generics were added in the 2.0 CLR, there is never a good reason to use ArrayList any more. It is only kept around for backward compatibility with older code, and should not be used when writing new code.
    Thanks for response, I know ArrayList is obsolete. However I want to do something with List<T> that seems to only work with ArrayList which is using the XML Seriaizer with it. Making sure derived classes' properties are included in the xml file as well. I'd like to know how to do that with List<T>.
    Leave luck to heaven.
  • 2/27/2009 7:11 PM In reply to

    Re: XML List Serialization

    Afraid I don't understand what your problem is. XmlSerializer works with List<T> exactly the same way as with ArrayList or a regular array. You use the same control attributes for any of these collection types.
    XNA Framework Developer - blog - homepage
  • 2/27/2009 7:35 PM In reply to

    Re: XML List Serialization


    I kept getting a InvalidOperationException before, I just rewrote it and it worked fine. I thought the exception was because the xml array attributes did not work with List<T>. Thanks Shawn!
    Leave luck to heaven.
Page 1 of 1 (5 items) Previous Next