Message 1 of 2
How to Serialize a class contains a list of points for C#
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have a simple class contains a string name, and a list of point3D.
[Serializable()] public class my_Volume:ISerializable { public my_Volume() { } public my_Volume(string name) { this.Name = name; } public string Name { get; set; } public List<Point3d> polyPoints { get; set; } //info is used to hold key value pairs. public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("Name", this.Name); info.AddValue("polyPoints", this.polyPoints); } public my_Volume(SerializationInfo info, StreamingContext context) { Name = (string)info.GetValue("Name",typeof(string)); polyPoints = (List<Point3d>)info.GetValue("polyPoints", typeof(List<Point3d>)); } }
I am trying to use a xml serializer to save it to a file. so I can retrieve it later on using the following code.
public void cmdTest() { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; var vol = new my_Volume("CB5-3"); vol.polyPoints = new List<Point3d> { new Point3d(0,0,0), new Point3d(0,1,0), new Point3d(0,0,1) }; Stream stream = File.Open("Volume.dat", FileMode.Create); XmlSerializer serializer = new XmlSerializer(typeof(my_Volume)); using (TextWriter tw = new StreamWriter(@"D:\myData.xml")) { serializer.Serialize(tw, vol); } vol = null; XmlSerializer deserializer = new XmlSerializer(typeof(my_Volume)); using(TextReader reader = new StreamReader(@"D:\myData.xml")) { object obj = deserializer.Deserialize(reader); vol = (my_Volume)obj; } ed.WriteMessage(vol.polyPoints[0].ToString()); ed.WriteMessage(vol.polyPoints[1].ToString()); ed.WriteMessage(vol.polyPoints[2].ToString()); }
Then I realize the list of points is not serialized. all the input becomes (0,0,0).
Any reasons why this happens?