在类上使用XmlSerializer时遇到问题:对象类型不是原始的

时间:2011-09-28 20:38:29

标签: c# .net mono xmlserializer

我在尝试序列化类时遇到此错误:“生成XML文档时出错。参数对象的类型不是原始的。”

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();
    }

有人看到我的错误吗?我不确定导致错误的原因。我是在Mono 2.10中写的。保存功能在另一个类中(您在此处的代码中看不到)。

1 个答案:

答案 0 :(得分:2)

更改

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

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