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. Serialize TreeView

Serialize TreeView

Scheduled Pinned Locked Moved C#
helpquestion
6 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.
  • S Offline
    S Offline
    SHaroz
    wrote on last edited by
    #1

    I have a custom treeview class with a couple of extra properties and methods. When I try to serialize an object of that type, I get a compiler error saying that TreeView has not been marked as serializable. Any suggestions as to how I can serialize this object? Any help would be greatly appreciated.

    J 1 Reply Last reply
    0
    • S SHaroz

      I have a custom treeview class with a couple of extra properties and methods. When I try to serialize an object of that type, I get a compiler error saying that TreeView has not been marked as serializable. Any suggestions as to how I can serialize this object? Any help would be greatly appreciated.

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      If you implement ISerializable you can control how the serialization happens; so that instead of serializing a TreeView; you serialize the parts you need to recreate it (ie the nodes, and your custom property). If the nodes aren't serializable, you'll have to do something special with them too. James "Java is free - and worth every penny." - Christian Graus

      S 2 Replies Last reply
      0
      • J James T Johnson

        If you implement ISerializable you can control how the serialization happens; so that instead of serializing a TreeView; you serialize the parts you need to recreate it (ie the nodes, and your custom property). If the nodes aren't serializable, you'll have to do something special with them too. James "Java is free - and worth every penny." - Christian Graus

        S Offline
        S Offline
        SHaroz
        wrote on last edited by
        #3

        I gave up on serializing the treeview, instead I just made a class with the same properties and meathod as before. The class has one extra property: a TreeNode. It is essentially just a root node for everything that would have been in the treeview. I implemented ISerializable like you said, and it serializes and deserializes perfectly except when the TreeNode has any other nodes in it (doh!). Do you know how I can get around this? Thanks for the help, Steve

        1 Reply Last reply
        0
        • J James T Johnson

          If you implement ISerializable you can control how the serialization happens; so that instead of serializing a TreeView; you serialize the parts you need to recreate it (ie the nodes, and your custom property). If the nodes aren't serializable, you'll have to do something special with them too. James "Java is free - and worth every penny." - Christian Graus

          S Offline
          S Offline
          SHaroz
          wrote on last edited by
          #4

          One more thing I thought of. I could recurse through the nodes. I would have to first serialize all the 'sub'nodes then clear and serialize the node itself. My problem then is: How do I deserialize them?

          J 1 Reply Last reply
          0
          • S SHaroz

            One more thing I thought of. I could recurse through the nodes. I would have to first serialize all the 'sub'nodes then clear and serialize the node itself. My problem then is: How do I deserialize them?

            J Offline
            J Offline
            James T Johnson
            wrote on last edited by
            #5

            All you should have to do is Serialize the root node; it should then serialize everything referenced in that node; including its child nodes. When you deserialize only worry about the first node; the rest should come with it. James "Java is free - and worth every penny." - Christian Graus

            S 1 Reply Last reply
            0
            • J James T Johnson

              All you should have to do is Serialize the root node; it should then serialize everything referenced in that node; including its child nodes. When you deserialize only worry about the first node; the rest should come with it. James "Java is free - and worth every penny." - Christian Graus

              S Offline
              S Offline
              SHaroz
              wrote on last edited by
              #6

              Try running this (and look at the y and z definitions): using System.IO; using System.Runtime; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters; using System.Runtime.Serialization.Formatters.Binary; . . . TreeNode x,y,z; x = new TreeNode("Root"); y = x.Nodes.Add("A"); //it only runs if you comment out these lines z = y.Nodes.Add("1"); //it only runs if you comment out these lines IFormatter formatter = new BinaryFormatter(); Stream mystream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(mystream, x); mystream.Close(); TreeNode root; mystream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read); root = (TreeNode)formatter.Deserialize(mystream); MessageBox.Show(root.Text); Thanks for the help :)

              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