开发者

Trouble using XmlSerializer on a class: Object type not primitive

I'm getting this error when trying to serialize a class: "There was an error generating the XML document. The type of the argument object is not primitive."

public class TaskData
{
    [XmlAttribute("Date")]
    public DateTime Date;

    public string Summary;
    public string Task;
    public int Priority; //1 Next, 2 Today, 3 This week, 4 This month, 5 This year.
    public bool InProgress;

    public TaskData() {}

    public TaskData(DateTime date, string summary, string task, int priority, bool inprogress)
    {
        Date = date;
        Summary = summary;
        Task = task;
        Priority = priority;
        InProgress = inprogress;
    }
}

public class Tasks
{
    [XmlArray("Tasks")]
    public List<TaskData> tasks;

    public Tasks(){tasks = new List<TaskData>();}
}

    static internal void Save(Tasks task)
    {
        string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        Save(task, Path.Combine(path, "tasks.xml"));
    }

    static private void Save(Tasks task, string path)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(TaskData));
        TextWriter writer = new StreamWriter(path);
        serializer.Serialize(writer, task);
        writer.Close();
    }

Do开发者_运维问答es anyone see my mistake? I'm not sure what is causing the error. I'm writing this in Mono 2.10. The Save functions are in another class (which you can't see in the code here).


Change

XmlSerializer serializer = new XmlSerializer(typeof(TaskData));

to

XmlSerializer serializer = new XmlSerializer(typeof(Tasks));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜