I'm having trouble making an client-server app. I'm working with Entity Framework(EF) and I need to serialize an object to send it via sockets that contains List attributes. I'm using XMLSerialization for the Serialization part.
My problem is: When I try to Serialize a new Survey object and the OPTIONs List is Empty I can Serialize the object SURVEY. But, when I add an OPTION object to the SURVEY.OPTIONS list I cannot Serialize the object.
One of the classes that EF auto-generates from the Entity-Relationship Diagram is:
public partial class SURVEY
{
public SURVEY()
{
this.OPCIONs = new List<OPTION>();
}
public int id_survey { get; set; }
public System.DateTime initial_date { get; set; }
public System.DateTime end_date { get; set; }
public virtual List<OPTION> OPTIONs { get; set; }
}
I'm using this Code for get a Survey from the Database:
DateTime actualDate = new DateTime().Today;
private static ComedorCaniaDBContext context = new ComedorCaniaDBContext();
Survey survey = context.SURVEYs.Create()
survey = (SURVEY)context.SURVEYs
.Include("Options")
.Where(e => e.initial_date < actualDate && e.end_date > actualDate)
.FirstOrDefault();
I'm using this Code for Serialization:
public static Byte[] ObjectToByteArray<T>(T obj)
{
try
{
using (MemoryStream ms = new MemoryStream())
{
XmlSerializer xmlS = new XmlSerializer(typeof(T));
xmlS.Serialize(ms, obj);
return ms.ToArray();
}
}
catch
{
return null;
}
}
I'll appreciate your help. Thanks.