Cannot add an object to a layer when it is alread

Hi,

Getting this: “Cannot add an object to a layer when it is already part of a different document’s or view’s layer.”

Im trying to serialize and deserialize some diagram data, mainly nodes and links.

This works:

       DiagramBuilder diagramBuilder = new DiagramBuilder(SelectedRevision.Id);

        _diagramViewMain.Document.Clear();
        _diagramViewMain.Document.Size = new SizeF(0, 0);
        _diagramViewMain.DocScale = 1;
        _diagramViewMain.Document.IsModified = false;

        DecisionDtosForDiagramData = diagramBuilder.GenerateDiagram(SelectedDiagram.Id);

        foreach (var node in diagramBuilder.DiagramNodes)
        {
            _diagramViewMain.Document.Add(node);
        }

        foreach (var link in diagramBuilder.DiagramLinks)
        {
            _diagramViewMain.Document.LinksLayer.Add(link);
        }

THIS DOES NOT WORK:

            BinaryFormatter formatter = new BinaryFormatter();

            // Deserialize the hashtable from the file and  
            // assign the reference to the local variable.
            draftDto = (DraftDto)formatter.Deserialize(fs);
            //DiagramBuilder diagramBuilder = new DiagramBuilder(draftDto.RevisionId);


            //diagramBuilder = draftDto.DiagramBuilder;

            //DecisionDtosForDiagramData = diagramBuilder.GenerateAll();
            _diagramViewMain.Document.Clear();
            _diagramViewMain.Document.Size = new SizeF(0, 0);
            _diagramViewMain.DocScale = 1;
            _diagramViewMain.Document.IsModified = false;

            //foreach (var layer in GoLayer)

            //_diagramViewMain.Document.LinksLayer = _diagramViewMain.Document.Layers.CreateNewLayerAfter(_diagramViewMain.Document.Layers.Default);
            _diagramViewMain.Layers.Remove(_diagramViewMain.Layers.Default);


            // Show all nodes
            foreach (var node in draftDto.DiagramNodes)
            {
                
                _diagramViewMain.Document.Add(node);
                //node.CreateEditor(diagramView);
            }

            // Show all links
            foreach (var link in draftDto.DiagramLinks)
            {
                _diagramViewMain.Document.LinksLayer.Add(link);
            }

This is the problem: foreach (var node in draftDto.DiagramNodes) - but its the same data. However if I change draftDto to diagramBuilder.DiagramNodes it works!

Help please

Your “THIS DOES NOT WORK” is removing the default layer.

Oh I tried to remove it, but if i comment that line out i still get the error. However I found another solution; and that is to serialize the whole GoDocument, and DeSerialize it instead. Works like a charm. Thanks I appreciate. Ill close this one.

We really don’t recommend using serialization as a storage format. XML is a
much better format. Serialization tends to break across versions.