我是一個非常新的MVC開發人員,並且在將我的類序列化到XML時遇到了一些問題。使用導航屬性進行XML序列化
目前,我有以下類:
public class UserClass
{
public int UserId{ get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool LogicalDelete { get; set; }
public virtual ICollection<Phone> Phone{ get; set; }
[XmlIgnore]
public virtual ICollection<EventList> Event{ get; set; }
}
public class Phone
{
public int TelefonosId { get; set; }
public string Phone{ get; set; }
public bool Mobile{ get; set; }
public int UsuarioId { get; set; }
public virtual UserClass User { get; set; }
}
的串行方法I'm從UserController中調用如下:
public void ExportToXML()
{
var data = mydb.User.ToList();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=testXML.xml");
Response.ContentType = "text/xml";
var serializer = new System.Xml.Serialization.XmlSerializer(data.GetType());
serializer.Serialize(Response.OutputStream, data);
}
接着而來的問題。當我嘗試序列化時,來自User類的導航屬性在「GetType」調用中給我一個反射類型錯誤。它工作得很好,沒有他們(我能夠正確導出用戶列表,沒有電話)。
我錯過了什麼?有什麼我可以做得更好嗎?
在此先感謝!
()異常,包括異常類型,消息的'輸出,追蹤和內部異常? – dbc
是你得到的錯誤'不能序列化System.Collections.Generic.ICollection 1 [[Phone]]類型的成員UserClass.Phone,因爲它是一個接口。這是我在這裏創建一個[mcve]時看到的錯誤:https://dotnetfiddle.net/NF9BpZ – dbc
是的,這正是錯誤我得到 –