Save custom shape to xml file

I have GoGeneralNode and GoIconicNode with custom shape drawn in source code or from .jpg file. How to save the information in xml file and load the shape later?

I think I miss something in RegisterTransformer method …

private void RegisterTransformer(GoXmlReaderWriterBase rw)
{
<span =“Apple-tab-span” style=“white-space:pre”> …
<span =“Apple-tab-span” style=“white-space:pre”>
GoGeneralNode gn = new GoGeneralNode();
gn.Initialize(null, “”, “”, “”, 0, 0);
bt = new GoXmlBindingTransformer(gn);
bt.IdAttributeUsedForSharedObjects = true;
bt.GeneratesPortsAsChildElements = true;
bt.BodyConsumesChildElements = true;
bt.AddBinding(“name”, “Image.Name”);
bt.AddBinding(“index”, “Image.Index”);
bt.AddBinding(“image”, “Icon”);
bt.AddBinding(“iconsize”, “Icon.Size”);
bt.AddBinding(“top”, “TopLabel.Text”);
bt.AddBinding(“bottom”, “BottomLabel.Text”);
bt.AddBinding(“loc”, “Location”);
rw.AddTransformer(bt);

        GoIconicNode gin = new GoIconicNode();
        gn.Initialize(null, "", "", "", 0, 0);
        bt = new GoXmlBindingTransformer(gin);
        //bt.IdAttributeUsedForSharedObjects = true;
        //bt.GeneratesPortsAsChildElements = true;
        //bt.BodyConsumesChildElements = true;
        bt.HandlesNamedPorts = true;
        bt.AddBinding("name", "Image.Name");
        bt.AddBinding("index", "Image.Index");
        bt.AddBinding("icon", "Icon");
        bt.AddBinding("iconsize", "Icon.Size");
        bt.AddBinding("loc", "Location");
        rw.AddTransformer(bt);

<span =“Apple-tab-span” style=“white-space:pre”> …

}

The first thing to do is look at the steps you execute to create the nodes. Then see if the AddBindings you have are sufficient to repeat those steps.

Hi Jake,
Thank you for your reply.

The GoIconicNode is OctagonalStar from DrawDemo project. I just try to save it into a XML file
Here is the code:

public MainForm() {
<span =“Apple-tab-span” style=“white-space:pre”> …
OctagonalStar octagonalstar = new OctagonalStar();
doc.Add(CreatePalletteNode(octagonalstar, “OctagonalStar”, width));
<span =“Apple-tab-span” style=“white-space:pre”> …
}

private static GoIconicNode CreatePalletteNode(GoObject obj, string label, float width) {
GoIconicNode iconNode = new GoIconicNode();
iconNode.Initialize(null, “”, AddBreak(label));
GoShape shape = obj as GoShape;
if (shape != null) {
shape.BrushColor = Color.White;
}
obj.Selectable = false;
obj.Size = new SizeF(24, 24);
iconNode.Icon = obj;
iconNode.Icon.Shadowed = true;
iconNode.Label.FontSize = 7;

  iconNode.Label.WrappingWidth = width;
  iconNode.Label.Wrapping = true;
  iconNode.Label.Clipping = true;
  iconNode.Label.StringTrimming = StringTrimming.EllipsisCharacter;
  return iconNode;
}

Can you show what you end up with in the XML and what it looks like when you try to restore the file?

Here is the xml file:

And I load from file as below:

private void Open()
{
OpenFileDialog dlg = new OpenFileDialog();
StreamReader file = null;
try
{
if (dlg.ShowDialog() == DialogResult.OK)
{
string loc = dlg.FileName;
GoXmlReader xr = new GoXmlReader();
RegisterTransformer(xr);
using (file = new StreamReader(loc))
{
myView.Document = (GoDocument)xr.Consume(file);
}
}
}
catch (Exception)
{
}
finally
{
if (file != null)
{
file.Close();
}
}
}

Well, your AddBinding code doesn’t match the XML you show later.

GoIconicNode.Icon is a GoObject. GoXml doesn’t know how to save as GoObject.

GoIconicNode.Image is Icon as GoImage. You can do Image.Name and Image.Index if those are appropriate.

GoIconicNode.Shape is Icon as GoShape (all 3 are synonyms for the same object in the node).

OctagonalStar is derived from GoShape, but it’s a custom object. GoXml doesn’t know how to do a “new OctagonalStar()”.

Note you can save a GoFigure in XML as “Shape.Figure”. If you were using a GoDrawing(GoFigure.EightPointedStar), you could save the shape that way. (at least I think that works, I don’t see a sample of it anywhere)

So does it means GoXML can not save an object with custom shape drawn by sourcecode like OctagonalStar?

Well, you can, but you have to write the code a slightly different way. Look at what FlowCharter does in GraphNode. It has a Kind property (with an enum GraphNodeKind) that will set the background shape. You can save
Kind in an XML, and restoring it will set the shape.

Thank you, Jake. I will look at it now, or at least I can save in binary file

Hi Jake, I have another question:
I want to create a node object with custom shape (drawn by myself in source code) and custom ports. I will create a new class MyNode inherited a Node of GoDiagram. But I wonder which node should I inherited from?
Which sample should I look at?

Can you post a drawing (with some links attached) ?

(click the Full Reply editor button to upload pics)

Note that we strongly recommend against using binary serialization for persisting your diagrams. You can read more about this issue in the User Guide.