microsoft / automatic-graph-layout

A set of tools for graph layout and viewing
Other
1.36k stars 307 forks source link

Graph.ReadGraphFromStream ignores subgraph #330

Open ControlFreakEv opened 2 years ago

ControlFreakEv commented 2 years ago

When only 1 subgraph is present in an XML stream the subgraph is not rendered in gviewer. The current workaround i've been using is to add a temporary subgraph before writing the graph to XML, then removing the temporary subgraph after reading the XML into a graph

See code for example

        Graph graph = new Graph("graph");
        var node1 = new Node("test1");
        graph.AddNode(node1);

        var subgraph = new Subgraph("subgraph1");
        graph.RootSubgraph.AddSubgraph(subgraph);
        subgraph.AddNode(node1);

        //uncommenting the next 2 lines will make subgraph1 appear. If left uncommented, subgraph1 will not appear
        //var subgraph2 = new Subgraph("delete") { IsVisible = false }; //IsVisible is not carried over in stream
        //graph.RootSubgraph.AddSubgraph(subgraph2);

        //write graph to xml
        string xml;
        using (var stream1 = new MemoryStream()) {
            graph.WriteToStream(stream1);
            stream1.Position = 0;
            using (var streamReader = new StreamReader(stream1)) {
                xml = streamReader.ReadToEnd();
            } 
        }

        //read graph from xml
        using (var stream = new MemoryStream()) {
            using (var streamWriter = new StreamWriter(stream)) {
                streamWriter.Write(xml);
                streamWriter.Flush();//otherwise you are risking empty stream
                stream.Position = 0;
                graph = Graph.ReadGraphFromStream(stream);
            }
        }

        //graph.RootSubgraph.RemoveSubgraph(graph.RootSubgraph.Subgraphs.Where(x => x.Id == "delete").FirstOrDefault());

        gViewer.Graph = graph;