Click here to Skip to main content
15,918,404 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi altogether,

I am frustrated...

I am copying a derived treenode to clipboard in this way:

C#
Clipboard.SetData("MyNode", Serialize(currentNode));


And I am trying to get it by:

C#
MyNode Node = (MyNode)DeSerialize(Clipboard.GetData("MyNode", ToString());


OPCNode is a derived class of TreeNode.

Serialize and Deserialize are functions like:

C#
private string Serialize(object objectToSerialize)
{
    string serialString = null;
    using (System.IO.MemoryStream ms1 = new System.IO.MemoryStream())
    {
        BinaryFormatter b = new BinaryFormatter();
        b.Serialize(ms1, objectToSerialize);
        byte[] arrayByte = ms1.ToArray();
        serialString = Convert.ToBase64String(arrayByte);
    }
    return serialString;
}

private object DeSerialize(string serializationString)
{
    object deserialObject = null;
    byte[] arrayByte = Convert.FromBase64String(serializationString);
    using (System.IO.MemoryStream ms1 = new System.IO.MemoryStream(arrayByte))
    {
        BinaryFormatter b = new BinaryFormatter();
        deserialObject = b.Deserialize(ms1);
    }
    return deserialObject;
}


The result is that I only get the "Node.Text" in a correct way.

"Node.ImageIndex" and all the members of MyNode are lost.

Any suggestion?

Thanks a lot in advance.

sja
Posted
Updated 6-Dec-13 4:54am
v2
Comments
BillWoodruff 6-Dec-13 11:55am    
It's important to know the context in which you are trying to perform a copy.

Is this copying to be done inside one running application ? WinForms ? WPF ? ASP.NET ? Or between two applications ?

1 solution

You do not serialize the TreeNode itself. You serialize the data in it, usually just a string, but you may also include the Tag property.

You don't have to use the formatters or even any serialization if the data is simple enough!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900