XMLSerialize 一个 ObservableCollection
我在可观察集合的 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,版本=1.0.0.0,文化=中立, PublicKeyToken=null]] 不能在此上下文中使用。
我正在使用 Visual Studio 2010,.NET 4.0 中的 WPF 应用程序。
I am having a problem in the xml serialization of observable collection.
Here is what I am serializing:
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; }
...
}
Here is how I serialize it:
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 , "...");
...
}
}
But an error occur in the xs.Serialize(wr, item);
line saying:
InvalidOperation Exception: There was an error generating the XML document.
Inner Exception: The type ScrumPresentor.ObservableCollection`1[[ScrumPresentor.UserStory,
ScrumPresentor, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]] may not be used in this context.
I am using Visual Studio 2010, WPF application in .NET 4.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 System.Xml.Serialization.XmlInclude 属性。我不确定我是否正确理解了您收集集合的意图,但假设这就是您想要的,这是一个可行的解决方案。输出如下代码:
上面的代码生成了这个文本文件:
Try using the System.Xml.Serialization.XmlInclude attribute. I'm not sure if I correctly understand your intent of a collection of collections, but assuming that is what you want, here is a working solution. The output follows the code:
The above code produced this text file: