List 序列化问题

wyb0026 2015-10-27 04:23:56
调用总是错 Lsit保存多种类难道不行

private void button1_Click(object sender, EventArgs e)
{

List<object> list = new List<object>();
list.Add(new A());
list.Add(new B());
list.SaveToXml(@"D:\aaa.xml");
}


这样就没问题

List<A> list = new List<A>();

但是我要A,B混在 有什么解决方法


序列化主程序总是错

public static void SaveToXml(this object obj, string filname)
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(obj.GetType());
using (FileStream stream = new FileStream(filname, FileMode.Create))
{
System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings()
{
Indent = true
};
using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(stream, settings))
{
xs.Serialize(writer, obj);

}
}
}




...全文
134 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧