Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to serialize a C# class [modified]

How to serialize a C# class [modified]

Scheduled Pinned Locked Moved C#
csharpxmljsontutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mail572352
    wrote on last edited by
    #1

    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's main() method. What I want is to create an instance of my object in my main() and just call the object's own deserialize() 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.

    V P 2 Replies Last reply
    0
    • M mail572352

      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's main() method. What I want is to create an instance of my object in my main() and just call the object's own deserialize() 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.

      V Offline
      V Offline
      vamsimohan21
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M mail572352

        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's main() method. What I want is to create an instance of my object in my main() and just call the object's own deserialize() 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.

        P Offline
        P Offline
        peterchen
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups