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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Multiple Object Type Serialization

Multiple Object Type Serialization

Scheduled Pinned Locked Moved C#
jsontutorial
4 Posts 2 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.
  • D Offline
    D Offline
    David Muir
    wrote on last edited by
    #1

    Hi All, I was wondering if someone could point me in the right direction. I am trying to figure out how to hold multiple object types in a single serialized file. E.g. If i was wanting to save settings class and a person class in a single serialized file, what would be the best method... also, how can you determine and objects type when deserializing the file. Thanks in advance, TF

    L 1 Reply Last reply
    0
    • D David Muir

      Hi All, I was wondering if someone could point me in the right direction. I am trying to figure out how to hold multiple object types in a single serialized file. E.g. If i was wanting to save settings class and a person class in a single serialized file, what would be the best method... also, how can you determine and objects type when deserializing the file. Thanks in advance, TF

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      string ConfigFile = @"c:\MyConfig.cfg";

      // Save
      int a = 10;

      string b = "hi";

      DataTable c = new DataTable();

      Stream StreamFile = File.Open(ConfigFile, FileMode.CreateNew);
      BinaryFormatter binformat = new BinaryFormatter();
      binformat.Serialize(StreamFile, a);
      binformat.Serialize(StreamFile, b);
      binformat.Serialize(StreamFile, c);
      StreamFile.Close();

      // Read
      Stream StreamFile = File.Open(ConfigFile, FileMode.Open);
      BinaryFormatter binformat = new BinaryFormatter();

      int a1 = (int)binformat.Deserialize(StreamFile);
      string b1 = (string)binformat.Deserialize(StreamFile);
      DataTable c1 = (DataTable)binformat.Deserialize(StreamFile);
      StreamFile.Close();

      I know nothing , I know nothing ...

      D 1 Reply Last reply
      0
      • L Lost User

        string ConfigFile = @"c:\MyConfig.cfg";

        // Save
        int a = 10;

        string b = "hi";

        DataTable c = new DataTable();

        Stream StreamFile = File.Open(ConfigFile, FileMode.CreateNew);
        BinaryFormatter binformat = new BinaryFormatter();
        binformat.Serialize(StreamFile, a);
        binformat.Serialize(StreamFile, b);
        binformat.Serialize(StreamFile, c);
        StreamFile.Close();

        // Read
        Stream StreamFile = File.Open(ConfigFile, FileMode.Open);
        BinaryFormatter binformat = new BinaryFormatter();

        int a1 = (int)binformat.Deserialize(StreamFile);
        string b1 = (string)binformat.Deserialize(StreamFile);
        DataTable c1 = (DataTable)binformat.Deserialize(StreamFile);
        StreamFile.Close();

        I know nothing , I know nothing ...

        D Offline
        D Offline
        David Muir
        wrote on last edited by
        #3

        Thanks :) Just another quick question which comes from the example; If you had; string ConfigFile = @"c:\MyConfig.cfg"; // Save int a = 1; int b = 2; Stream StreamFile = File.Open(ConfigFile, FileMode.CreateNew); BinaryFormatter binformat = new BinaryFormatter(); binformat.Serialize(StreamFile, a); binformat.Serialize(StreamFile, b); StreamFile.Close(); ... to create the file, when reading back from the file, how can you indentify which int you would be casting back? Cheers TF

        L 1 Reply Last reply
        0
        • D David Muir

          Thanks :) Just another quick question which comes from the example; If you had; string ConfigFile = @"c:\MyConfig.cfg"; // Save int a = 1; int b = 2; Stream StreamFile = File.Open(ConfigFile, FileMode.CreateNew); BinaryFormatter binformat = new BinaryFormatter(); binformat.Serialize(StreamFile, a); binformat.Serialize(StreamFile, b); StreamFile.Close(); ... to create the file, when reading back from the file, how can you indentify which int you would be casting back? Cheers TF

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Hi , you are welcome for your Question : if you Save your variables like this order ... int a = 1; int b = 9; int c = 3; then you can read JUST like this a = (int)Deserlaize ( .... ) b = (int)Deserlaize ( .... ) c = (int)Deserlaize ( .... ) in the same order , and the output will be a = 1 , b = 9 , c = 3 in other word you can't read (int b) , with out reading ( int a ) .... P.S : I am sorry , I can't found the right word to present this but may be ( Book1 Above Book2 Above Book3 ) .... you can't get book2 , with out (moving or push away ) book1 .... I am really sorry for this bad english .... but if you got the idea , please let me know , I will be happy :rose:

          I know nothing , I know nothing ...

          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