0

i am serializing a treeview with its nodes , into xml file.

And when i deserialize back from the xml file.

I can't find the whole content of my treeview in the XML.

The problem, that in this treeview I have added a tag with specific type that i got from a cognex sdk. I guess this type is not serializable?

I can't find the tag , i putted before serializing it , here is how I serialize the object:

public static void SaveTree(TreeView tree, string filename)
{
    using (Stream file = File.Open(filename, FileMode.Create))
    {
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(file, tree.Nodes.Cast<TreeNode>().ToList());
    }
}

public static TreeView LoadTree(string filename)
{
    TreeView tree=new TreeView();

    using (Stream file = File.Open(filename, FileMode.Open))
    {
        BinaryFormatter bf = new BinaryFormatter();
        object obj = bf.Deserialize(file);

        TreeNode[] nodeList = (obj as IEnumerable<TreeNode>).ToArray();
        tree.Nodes.AddRange(nodeList);
    }

    return tree;
}

I guess the type is not serialisable , so what should i do ? How can I serialize this object?

Liam
  • 27,717
  • 28
  • 128
  • 190
maher
  • 43
  • 8
  • Whats the question? – Liam Nov 27 '17 at 11:28
  • Please confirm what type of object you are placing into the Tag and if that object is of a 'local' type confirm if that type is serializable. – tolanj Nov 27 '17 at 12:21
  • There is no XML in this seriilzation. Your using a [BinaryFormatter](https://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter(v=vs.110).aspx). This creates a binary file, not an XML file as you state – Liam Nov 27 '17 at 14:10
  • Possible duplicate of [Is it possible to do .NET binary serialization of an object when you don't have the source code of the class?](https://stackoverflow.com/questions/13166105/is-it-possible-to-do-net-binary-serialization-of-an-object-when-you-dont-have) – Liam Nov 27 '17 at 14:12

1 Answers1

1

First of all your code serializes content in binary format instead of xml but it would not be a problem while serializing and deserializing.

Please be sure that class of object in the Tag property is also serializable or inherits from MarshallByObjectRef. If the class is not serializable then you cannot serialize or deserialize it.

[EDIT]

As per the source code for the Deserialize method. TreeNode has some 'defensive' code around the serialization of Tag (which is a Property wrapping the userData field):

  if (userData != null && userData.GetType().IsSerializable) {
            si.AddValue("UserData", userData, userData.GetType());

So Tag will only be serialized IF it is an explicitly serializable type. Thus, if you are otherwise able to (De)Serialize the TreeNodes correctly it seems likely that you are assigning a non-serializable object to Tag which then gets stripped away by the above code (the only alternative for the TreeNode serialization would be to throw an error).

Liam
  • 27,717
  • 28
  • 128
  • 190
Mutlu Kaya
  • 129
  • 7
  • @Liam, Why do you think that it is not full answer ? As I said, a serializable object must be set to tag property of TreeNode class to be able to serialize it. If you have any other mention, please share with me. – Mutlu Kaya Nov 27 '17 at 11:32
  • @Liam Hmm...agreed. My fault, will delete it. But most of the time it seems that the new users are quite rep greedy. Or i might overreact since they probably dont know common behaving here and how to handle what. – L. Guthardt Nov 27 '17 at 11:43
  • @Liam, Thank you,I will take your advice. Please note that i already executed the code and saw it' s working, i understood the problem and after that I wrote the response. And I think the problem is Tag property of TreeNode Class in the Windows Forms Application environment because he said "I can't find the tag , i putted before serializing it". Anyway, thank you again. – Mutlu Kaya Nov 27 '17 at 11:48
  • 1
    I actually think this is a good answer, if a little lacking in details. I have expanded it somewhat to avoid an essentially duplicate answer of my own. – tolanj Nov 27 '17 at 11:58
  • @MutluKaya hello , thanks i got the information.well am not sure whether the type in the tag is serialisable or not .Am using the cognex sdk system , it is a QR code scanner , i think this type is not serialisable that's why .What shoulld i do in this case.? – maher Nov 27 '17 at 12:44
  • The edit has definitely improved this answer – Liam Nov 27 '17 at 12:52
  • @maher, you can try to implement custom serializer for the scanner class but I' m not sure that it is possible or not. in deserialization process, you need to set the scanner class' state same as the state before serialization that' s why I m not sure. – Mutlu Kaya Nov 27 '17 at 12:54
  • @maher rather than discussing this in comments why don't you edit you question. Then you might get it re-opened. – Liam Nov 27 '17 at 12:55
  • @Liam already done – maher Nov 27 '17 at 13:30