By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,685 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,685 IT Pros & Developers. It's quick & easy.

XmlSerialization of objects in asp.net platform

P: 1
Im trying to serialize to XMl some objects and it gives me the following errors :
An unhandled exception of type 'System.InvalidOperationException' 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(typeof(Actor));
TextWriter tw = new
StreamWriter(@"C:\Users\admin\Desktop\Exercises\Se rialization\program.xml");
serializer.Serialize(tw, actor);

Actor bill = new Actor();
bill.Actor_Id = "10"; bill.Actor_Name = "Bill";
bill.Actor_LastName = "Aktinson";
bill.ActorPhoto = System.Text.Encoding.UTF8.GetBytes(" ");
bill.ActorBirthDate = DateTime.Parse("1990/01/15");
tw.Close();


XmlSerializer deserializer = new XmlSerializer(typeof(Actor));
TextReader reader = new StringReader(@"C:\Users\admin\Desktop\Exercises\Se rialization\program.xml");
object obj = deserializer.Deserialize(reader) as object;
actor = (Actor)obj;
/*Actor Actor;
Console.WriteLine("ActorId: " + Actor.Actor_Id);
Console.WriteLine("ActorName: " + Actor.Actor_Name);
Console.WriteLine("ActorLastName: " + Actor.Actor_LastName);
Console.WriteLine("ActorPhoto: " + Actor.ActorPhoto);
Console.WriteLine("ActorBirthday: " + Actor.ActorBirthDate);
*/
reader.Close();



}

Actor.cs code :
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Serialization
{


[Serializable()]
public partial class Actor : ISerializable
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("M icrosoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors" )]


public Actor()
{
this.Episode_Actors = 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.Diagnostics.CodeAnalysis.SuppressMessage("M icrosoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Episode_Actors> Episode_Actors { get; set; }

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("A_ID", Actor_Id);
info.AddValue("A_Name", Actor_Name);
info.AddValue("A_LastName",Actor_LastName);
info.AddValue("A_Photo", ActorPhoto);
info.AddValue("A_BirthDate", ActorBirthDate);
}
public Actor(SerializationInfo info, StreamingContext context)
{
Actor_Id = (string)info.GetValue("A_ID", typeof(string));
Actor_Name = (string)info.GetValue("A_Name", typeof(string));
Actor_LastName = (string)info.GetValue("A_LastName", typeof(string));
ActorPhoto= (byte[])info.GetValue("A_Photo", typeof(byte[]));
ActorBirthDate = (DateTime)info.GetValue("A_ID", typeof(DateTime));
}
}

}
Jan 5 '19 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.