2
Car car1 = new Car();
car1.CarMake = "Ford";
car1.CarModel = "Mustang GT";

//1. Serialization to a file
Stream stream = File.Open("CarInformation.txt", FileMode.OpenOrCreate);
BinaryFormatter bform = new BinaryFormatter();
bform.Serialize(stream, car1);
stream.Close();

//2. Serialization to an XML file
XmlSerializer xmlSer = new XmlSerializer(typeof(Car));
TextWriter writer = new StreamWriter("CarInformation.xml");
xmlSer.Serialize(writer, car1);

Why does not serialization to an XML require an object 'Car' to be declared as 'Serializable' whereas the first set of statements throws a SerializationException?

Afz
  • 77
  • 2
  • 16

0 Answers0