Serialization of a complicated class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Just D

    Serialization of a complicated class

    Hi,

    I need to write a serialization (to XML string) and restoring (from XML
    string) of a very complicated object. The object uses a few classes, one
    class has two ArrayLists, etc. The general structure supposes to have 2-3
    levels.

    What's easier, to write a serialization method for each class used by the
    main class and a short method to gather all these XML strings together into
    one complex string or to use some trick and serialize the whole structure
    into one XML?

    I expect that this structure will be changing and the deserialization should
    read all previous versions, so that Ser./Deser. methods should be created
    now to support all further versions.

    Did anybody write these methods for a very complicated class? What's
    experience?

    Thanks,
    Dmitri

  • John Wood

    #2
    Re: Serialization of a complicated class

    You could try using SOAP to serialize the class to see if it meets your
    needs.

    To serialize to soap you first need to add a reference to the SOAP formatter
    (System.Runtime .Serialization. Formatters.Soap ).

    The use some code like this:

    SoapFormatter form = new SoapFormatter() ;
    form.Serialize( st, myInstance);

    Where st is a stream (a file stream, or memory stream or something), and
    myInstance is the instance of your object.

    Then to deserialize the object, use something like:

    myInstace = (MyClass) form.Deserializ e(st);

    SOAP generally works quite well with version changes.

    Hope that helps.
    John

    "Just D" <no@spam.please > wrote in message
    news:By6Bc.2066 $DQ4.706@fed1re ad05...[color=blue]
    > Hi,
    >
    > I need to write a serialization (to XML string) and restoring (from XML
    > string) of a very complicated object. The object uses a few classes, one
    > class has two ArrayLists, etc. The general structure supposes to have 2-3
    > levels.
    >
    > What's easier, to write a serialization method for each class used by the
    > main class and a short method to gather all these XML strings together[/color]
    into[color=blue]
    > one complex string or to use some trick and serialize the whole structure
    > into one XML?
    >
    > I expect that this structure will be changing and the deserialization[/color]
    should[color=blue]
    > read all previous versions, so that Ser./Deser. methods should be created
    > now to support all further versions.
    >
    > Did anybody write these methods for a very complicated class? What's
    > experience?
    >
    > Thanks,
    > Dmitri
    >[/color]


    Comment

    • Marc Selis

      #3
      Re: Serialization of a complicated class

      You can instruct the XML Serializer how to translate your properties into XML using attributes:

      [Serializable]
      public class Order
      {

      public Order() //parameterless constructor is needed for deserialization
      {
      }

      [XmlAttribute("D ate")]
      public string Date
      { get{ .... } set{....}}

      private ArrayList mLines = new ArrayList();
      [XmlArray("Lines ")]
      [XmlArrayElement ("Line",typeof( OrderLine))]
      public IList OrderLines
      {
      get
      {
      return mLines;
      }
      }


      }



      If you add new properties to your class, the deserializer will still be able to read old files.
      These properties will retain their default value.
      If you remove properties, the deserializer will ignore the XML nodes he doesn't recognize.
      Just make sure you don't change the return types of properties, because then you will get exceptions.

      Regards,
      Marc Selis


      "Just D" <no@spam.please > wrote in message news:By6Bc.2066 $DQ4.706@fed1re ad05...[color=blue]
      > Hi,
      >
      > I need to write a serialization (to XML string) and restoring (from XML
      > string) of a very complicated object. The object uses a few classes, one
      > class has two ArrayLists, etc. The general structure supposes to have 2-3
      > levels.
      >
      > What's easier, to write a serialization method for each class used by the
      > main class and a short method to gather all these XML strings together into
      > one complex string or to use some trick and serialize the whole structure
      > into one XML?
      >
      > I expect that this structure will be changing and the deserialization should
      > read all previous versions, so that Ser./Deser. methods should be created
      > now to support all further versions.
      >
      > Did anybody write these methods for a very complicated class? What's
      > experience?
      >
      > Thanks,
      > Dmitri
      >[/color]

      Comment

      Working...