How to serialize a C# class [modified]
-
I want to make my class serializable. It only has a few fields and I want to store the settings as XML. At the moment I use an
XmlSerializer
to serialise and deserialize the object but I have all the code in my program'smain()
method. What I want is to create an instance of my object in mymain()
and just call the object's owndeserialize()
method. What's the best way of doing this? Currently:MyObject obj;
XmlSerializer s = new XmlSerializer(typeof(MyObject));
TextReader r = new StreamReader("myobject.xml");
weights = (MyObject)s.Deserialize(r);
r.Close();Prefer:
MyObject obj = new MyObject;
obj.deserialize();I just want to say as well that there are so many examples out there that ignore the idea of object-oriented programming. If an object is serializable then that object should handle its own serialization. The necessary serialization code shouldn't end up in some separate method in the
main()
function. -
I want to make my class serializable. It only has a few fields and I want to store the settings as XML. At the moment I use an
XmlSerializer
to serialise and deserialize the object but I have all the code in my program'smain()
method. What I want is to create an instance of my object in mymain()
and just call the object's owndeserialize()
method. What's the best way of doing this? Currently:MyObject obj;
XmlSerializer s = new XmlSerializer(typeof(MyObject));
TextReader r = new StreamReader("myobject.xml");
weights = (MyObject)s.Deserialize(r);
r.Close();Prefer:
MyObject obj = new MyObject;
obj.deserialize();I just want to say as well that there are so many examples out there that ignore the idea of object-oriented programming. If an object is serializable then that object should handle its own serialization. The necessary serialization code shouldn't end up in some separate method in the
main()
function.I may not be sure about my reply but we can create a class with the structure and define the properties for each field in the class ex: class MyObject { int _intVar; [XmlElement("IntVar")] public int IntVar { get { return _intVar; } set { _intVar = value; } } } where the xml file contains the IntVar element. Using the XmlElement related attributes for serializing and deserializing can be achieved.
ALL THE BEST
-
I want to make my class serializable. It only has a few fields and I want to store the settings as XML. At the moment I use an
XmlSerializer
to serialise and deserialize the object but I have all the code in my program'smain()
method. What I want is to create an instance of my object in mymain()
and just call the object's owndeserialize()
method. What's the best way of doing this? Currently:MyObject obj;
XmlSerializer s = new XmlSerializer(typeof(MyObject));
TextReader r = new StreamReader("myobject.xml");
weights = (MyObject)s.Deserialize(r);
r.Close();Prefer:
MyObject obj = new MyObject;
obj.deserialize();I just want to say as well that there are so many examples out there that ignore the idea of object-oriented programming. If an object is serializable then that object should handle its own serialization. The necessary serialization code shouldn't end up in some separate method in the
main()
function.Construction from serialized data can be substantially cheaper than default construction and replacing the properties with deserialized data.
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighist