【发布时间】:2014-02-27 12:12:35
【问题描述】:
我正在序列化 Linq 对象以使用 XmlSerializer 但我得到了
"反映类型时出现错误 'System.Collections.Generic.List`1"
我用 Serilizable 属性扩展了 Linq 对象,我在这里做错了什么?
[Serializable]
public partial class Customer
{
}
[Serializable]
public partial class Comment
{
}
[Serializable]
public class CustomerArchive
{
public Customer MyCustomer{ get; set; }
public Comment MyComment { get; set; }
}
internal static void Serialize(IEnumerable<CustomerArchive> archive, string fileName)
{
var serializer = new XmlSerializer(typeof(List<CustomerArchive>)); // Error
using (var stream = File.Create(fileName))
{
using (var sw = new StreamWriter(stream))
using (var writer = new XmlTextWriter(sw)
{
Indentation = 5,
Formatting = Formatting.Indented
})
serializer.Serialize(writer, new List<CustomerArchive>(archive));
}
}
【问题讨论】:
-
你在哪一行得到错误
-
var serializer = new XmlSerializer(typeof(List
));在这条线上..:-( -
内部异常告诉你什么?继续关注内部异常,直到到达最后一个。
-
你检查过内部异常吗?
-
类似错误{“反映类型'CustomerArchive'的错误。”}反映属性'MyCustomer'的错误
标签: c# linq serialization xml-serialization