XML serializing problems

Hey guys I keep getting an error when I try to serialize a list of a class that I made

[Serializable]	
public class DialogItem
{
	public bool 			show = true;
	public Speakers 		speaker = Speakers.NPC;
	public string			AudioClipRef;
	public string 			subtitle = "(enter subtitle text)";
	public string			AnimationToPlay;
        [XmlIgnore]
        public AudioClip        spoken = null;
	public Waits 			wait = Waits.Audio;
    //public bool 			show_camera = true;
    //public Perspectives 	perspective = Perspectives.Classic_Two_Shot;
    //public Angles 			angle = Angles.level;
    //public Distances		distance = Distances.medium;
    //public Sides 			side = Sides.left;
		
	public bool 			is_branch = false;
	public List<String> 	choice;
	public List<int> 		jump;

    public List<NodeAction> onNodeFocusedActions = new List<NodeAction>(),
                            onNodeLeaveActions   = new List<NodeAction>();
		
    //public bool 			has_action = false;
    //public string 			ActionMethod;
    //public string 			ActionParameter;

	public DialogItem()
	{

	}
	
	public void Initialize()
	{
		if(AudioClipRef != String.Empty)
		{
			spoken = (AudioClip)Resources.Load(AudioClipRef);
		}
	
		
	}

#if UNITY_EDITOR
    public void BuildForSerialize()
    {
       
        AudioClipRef    = AssetDatabase.GetAssetPath(spoken);
    }
#endif
}

The part throwing the error is the List it keeps telling me that my node actions are not primitive objects. Here is the code for the node actions.

[System.Serializable]
public class NodeAction
{

    public void Initilize(GameObject owner)
    {
        Debug.Log("Adding Component with type of: " + GetType());
        NodeActionComponent.LinkNodeAction(owner, this);
    }

#if UNITY_EDITOR
    public virtual void OnInspectorGUI(){}
#endif//end unity editor region
}

[System.Serializable]
public class NodeActionLookAt : NodeAction
{
    public int target;

#if UNITY_EDITOR
    public override void OnInspectorGUI()
    {
        target = EditorGUILayout.IntField("Test", target);
    }
#endif
}

Im really at a loss and have no idea what to do. The exact error is: InvalidOperationException: The type of the argument object ‘NodeActionLookAt’ is not primitive. Any help would be appreciated. Thanks!

In case anyone else stumbles on this problem. The solution I found was that when you are serializing a list of derived classes in addition to have the [Serializable] attribute you have to use [XmlInclude(typeof(MyDerivedClass))] Tag. c# - XML Serialize generic list of serializable objects with abstract base class - Stack Overflow for more information.

There are two solutions. One is to mark up the base class with a list of possible derived types that you might want to serialize based on it, by adding XmlInclude attributes like this one:

[Serializable]
[XmlInclude(typeof(NodeActionLookAt))]
class NodeAction
{
    ...

This is architecturally kind of weak because it requires the base class to know all possible descendents. The other approach is to pass a second argument to Serialize(), which should be an array of “extra” types that you know might crop up during the serialization:

serializer.Serialize(rootObjectToSerialize, new Type[] { typeof(NodeActionLookAt) });

This is, in some ways, better because the knowledge of which types might crop up is moved from the base class (potentially in a library, so not modifiable) to the point of code issuing the serialize call (which may be better placed to know the types).

As an ultimate measure, you can use reflection yourself to walk through what would be serialized and form a type list to pass.

I’m not sure exactly why the serializer can’t do this automatically.