[英]Error while serializing a class to XML
I am serializing Linq object to using XmlSerializer but I am getting 我正在将Linq对象序列化为使用XmlSerializer,但是我正在
"There was an error reflecting type 'System.Collections.Generic.List`1"
“发生错误,反映了类型'System.Collections.Generic.List`1”
I have extended the Linq object with Serilizable attributes, something that m I doing incorrect here? 我用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));
}
}
I think you have some properties inside MyCustomer
class that cannot be serialized by the XML Serializer
. 我认为您在
MyCustomer
类中具有一些不能由XML Serializer
属性。 Like a generic Dictionary<>
for example. 例如,像通用的
Dictionary<>
。
Put [XmlIgnore]
attribute on those properties and try again. 将
[XmlIgnore]
属性放在这些属性上,然后重试。 If after that your code works you will simply need to make some surrogate properties that would serialize to XML and back without any issues. 如果在此之后您的代码能够正常工作,您只需要制作一些代理属性即可将其序列化为XML,然后再返回就不会出现任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.