Decode Xml in mxGraph.Net

I want to perform an operation, which is the inverse of what I do when generating the xmlString in mxGraph ().

mxGraph graph = new mxGraph();
        Object parent = graph.GetDefaultParent();

        graph.Model.BeginUpdate();
        try
        {
            Object v1 = graph.InsertVertex(parent, null, "Hello,", 20, 20, 80, 30);
            Object v2 = graph.InsertVertex(parent, null, "World!", 200, 150, 80, 30);
            Object e1 = graph.InsertEdge(parent, null, "Edge", v1, v2);
        }
        finally
        {
            graph.Model.EndUpdate();
        }

        mxCodec codec = new mxCodec();
        Xml = mxUtils.GetXml(codec.Encode(graph.Model));
        var xmlString = mxUtils.GetXml(xml);

I am trying to perform the reverse operation.

        XmlDocument doc = mxUtils.ParseXml(xmlString);

        mxGraph graphNew = new mxGraph();

        var decoder = new mxCodec(doc);



        decoder.Decode(doc, graphNew.Model);

        Object parentNew = graphNew.GetDefaultParent();

But the parentNew object has no children.

+4
source share
2 answers

I just ran into this myself and was able to solve it.

decoder.Decode(doc, graphNew.Model);

it should be:

decoder.Decode(doc.DocumentElement, graphNew.getModel());

In addition to doc.DocumentElementyou also need to use graphNew.getModel(), noe just graphNew.model.

According to this documentation:

https://jgraph.imtqy.com/mxgraph/docs/js-api/files/io/mxCodec-js.html

In addition, you do not need to create a new schedule. Insert an existing graph into the second parameter Decode.

0
source

this code:

decoder.Decode(doc, graphNew.Model);

should be replaced with the following code:

decoder.Decode(doc.DocumentElement, graphNew.Model);
-1
source

Source: https://habr.com/ru/post/1619165/


All Articles