Im trying to serialize to XMl some objects and it gives me the following errors :
An unhandled exception of type 'System.Invalid OperationExcept ion' occurred in System.Xml.dll
Additional information: There was an error reflecting type 'Serialization. Actor'.
Here my code:
class Program
{
static void Main(string[] args)
{
Actor actor = new Actor();
XmlSerializer serializer = new
XmlSerializer(t ypeof(Actor));
TextWriter tw = new
StreamWriter(@" C:\Users\admin\ Desktop\Exercis es\Serializatio n\program.xml") ;
serializer.Seri alize(tw, actor);
Actor bill = new Actor();
bill.Actor_Id = "10"; bill.Actor_Name = "Bill";
bill.Actor_Last Name = "Aktinson";
bill.ActorPhoto = System.Text.Enc oding.UTF8.GetB ytes(" ");
bill.ActorBirth Date = DateTime.Parse( "1990/01/15");
tw.Close();
XmlSerializer deserializer = new XmlSerializer(t ypeof(Actor));
TextReader reader = new StringReader(@" C:\Users\admin\ Desktop\Exercis es\Serializatio n\program.xml") ;
object obj = deserializer.De serialize(reade r) as object;
actor = (Actor)obj;
/*Actor Actor;
Console.WriteLi ne("ActorId: " + Actor.Actor_Id) ;
Console.WriteLi ne("ActorName: " + Actor.Actor_Nam e);
Console.WriteLi ne("ActorLastNa me: " + Actor.Actor_Las tName);
Console.WriteLi ne("ActorPhoto : " + Actor.ActorPhot o);
Console.WriteLi ne("ActorBirthd ay: " + Actor.ActorBirt hDate);
*/
reader.Close();
}
Actor.cs code :
using System;
using System.Collecti ons.Generic;
using System.Runtime. Serialization;
namespace Serialization
{
[Serializable()]
public partial class Actor : ISerializable
{
[System.Diagnost ics.CodeAnalysi s.SuppressMessa ge("Microsoft.U sage", "CA2214:DoNotCa llOverridableMe thodsInConstruc tors")]
public Actor()
{
this.Episode_Ac tors = new HashSet<Episode _Actors>();
}
public string Actor_Id { get; set; }
public string Actor_Name { get; set; }
public string Actor_LastName { get; set; }
public byte[] ActorPhoto { get; set; }
public Nullable<System .DateTime> ActorBirthDate { get; set; }
[System.Diagnost ics.CodeAnalysi s.SuppressMessa ge("Microsoft.U sage", "CA2227:Collect ionPropertiesSh ouldBeReadOnly" )]
public virtual ICollection<Epi sode_Actors> Episode_Actors { get; set; }
public void GetObjectData(S erializationInf o info, StreamingContex t context)
{
info.AddValue(" A_ID", Actor_Id);
info.AddValue(" A_Name", Actor_Name);
info.AddValue(" A_LastName",Act or_LastName);
info.AddValue(" A_Photo", ActorPhoto);
info.AddValue(" A_BirthDate", ActorBirthDate) ;
}
public Actor(Serializa tionInfo info, StreamingContex t context)
{
Actor_Id = (string)info.Ge tValue("A_ID", typeof(string)) ;
Actor_Name = (string)info.Ge tValue("A_Name" , typeof(string)) ;
Actor_LastName = (string)info.Ge tValue("A_LastN ame", typeof(string)) ;
ActorPhoto= (byte[])info.GetValue( "A_Photo", typeof(byte[]));
ActorBirthDate = (DateTime)info. GetValue("A_ID" , typeof(DateTime ));
}
}
}