XMLSerialize一个ObservableCollection

时间:2011-12-26 06:17:15

标签: c# .net-4.0 observablecollection xmlserializer

我在observable集合的xml序列化中遇到问题。

这是我正在序列化的内容:

public enum Status { Pending, Active, Completed, Cancelled }

public abstract class Entity : INotifyPropertyChanged
{
    ...
}

public class UserStory : Entity
{
    public uint StoryID { get; set; }
    public Status Status { get; set; }
    ...
    public ObservableCollection<Task> Tasks { get; set; }
}

public class Task : Entity
{
    public uint TaskID { get; set; }
    ...
}

以下是我如何序列化它:

public static void SerializeObjectToXML<T>(T item, string FilePath)
{
    XmlSerializer xs = new XmlSerializer(typeof(T));
    using (StreamWriter wr = new StreamWriter(FilePath))
    {
        xs.Serialize(wr, item);
    }
}

public class Main()
{
    ObservableCollection<UserStory> UserStories { get; set; }

    void Main()
    {
        ...
        ObservableCollection<object> Document = new ObservableCollection<object>();
        Document.Add(UserStories);
        SerializeObjectToXML<ObservableCollection<object>>(Document , "...");
        ...
    }
}

xs.Serialize(wr, item);行中出现错误:

  

InvalidOperation异常:生成XML文档时出错。   内部异常:类型ScrumPresentor.ObservableCollection`1 [[ScrumPresentor.UserStory,   ScrumPresentor,Version = 1.0.0.0,Culture = neutral,   可能不会在此上下文中使用PublicKeyToken = null]。

我在.NET 4.0中使用Visual Studio 2010,WPF应用程序。

1 个答案:

答案 0 :(得分:22)

尝试使用 System.Xml.Serialization.XmlInclude 属性。我不确定我是否正确理解了你收集的集合的意图,但假设这是你想要的,这是一个有效的解决方案。输出遵循以下代码:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Xml.Serialization;

namespace StackoverflowXxmlSerialize
{

    public enum Status { Pending, Active, Completed, Cancelled }

    [System.Xml.Serialization.XmlInclude(typeof(UserStory))]
    [System.Xml.Serialization.XmlInclude(typeof(Task))]    
    public abstract class Entity : INotifyPropertyChanged 
    {
        public event PropertyChangedEventHandler PropertyChanged;
    }


    public class UserStory : Entity
    {
        public uint StoryID { get; set; }
        public Status Status { get; set; }
        public ObservableCollection<Task> Tasks { get; set; }
    }

    public class Task : Entity 
    {
        public uint TaskID { get; set; }
    }

    class Util
    {
        public static void SerializeObjectToXML<T>(T item, string FilePath)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            using (StreamWriter wr = new StreamWriter(FilePath))
            {
                xs.Serialize(wr, item);
            }
        }
    }



    public class TestSerialize
    {
        static ObservableCollection<Entity> UserStories { get; set; }

        public static void RunTest()
        {
            UserStories = new ObservableCollection<Entity> { 
                new UserStory  {
                StoryID = 127,
                Status = Status.Active,
                Tasks = new ObservableCollection<Task>{new Task { TaskID = 11 }, new Task { TaskID = 12 }}
                },

                new UserStory  {
                StoryID = 128,
                Status = Status.Cancelled,
                Tasks = new ObservableCollection<Task>{new Task { TaskID = 13 }, new Task { TaskID = 14 }}
                },

                new UserStory  {
                StoryID = 129,
                Status = Status.Completed,
                Tasks = new ObservableCollection<Task>{new Task { TaskID = 9 }, new Task { TaskID = 10 }}
                },
            };


            ObservableCollection<ObservableCollection<Entity>> Document
                = new ObservableCollection<ObservableCollection<Entity>>();

            Document.Add(UserStories);
            Util.SerializeObjectToXML<ObservableCollection<ObservableCollection<Entity>>>(Document, @"d:\tmp\junk.txt");

        }
    }

}

上面的代码生成了这个文本文件:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfArrayOfEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ArrayOfEntity>
    <Entity xsi:type="UserStory">
      <StoryID>127</StoryID>
      <Status>Active</Status>
      <Tasks>
        <Task>
          <TaskID>11</TaskID>
        </Task>
        <Task>
          <TaskID>12</TaskID>
        </Task>
      </Tasks>
    </Entity>
    <Entity xsi:type="UserStory">
      <StoryID>128</StoryID>
      <Status>Cancelled</Status>
      <Tasks>
        <Task>
          <TaskID>13</TaskID>
        </Task>
        <Task>
          <TaskID>14</TaskID>
        </Task>
      </Tasks>
    </Entity>
    <Entity xsi:type="UserStory">
      <StoryID>129</StoryID>
      <Status>Completed</Status>
      <Tasks>
        <Task>
          <TaskID>9</TaskID>
        </Task>
        <Task>
          <TaskID>10</TaskID>
        </Task>
      </Tasks>
    </Entity>
  </ArrayOfEntity>
</ArrayOfArrayOfEntity>