List 序列化问题

wyb0026 wyb0026 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
调用总是错 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);

}
}
}




...全文
评论
收藏 已收藏
分享
返回顶部
wyb0026
C# 111,130 成员 642,541 内容