XML序列化不需要对象的Serializable关键字

时间:2015-02-17 10:07:57

标签: c# serialization xml-serialization serializable

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);

为什么不对XML进行序列化需要一个对象' Car'被宣布为' Serializable'而第一组语句抛出SerializationException?

0 个答案:

没有答案