SimpleFlowDiagramLib: Simple C# library to Serialize Graph to Xml (And Vice Versa)

In continuation from previous article, we’d explore “SimpleFlowDiagramLib” capability to serialize Graph to Xml (And Vice versa). And, why do we want to do that? For example, wire down a graph to/from Web Services consumed by Java client for example.

Source code:

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SimpleFlowDiagramLib;

namespace DemoSimpleFlowDiagramLib
    class Program
static void Main(string[] args) { // STEP 1. Generate the nodes, render it to XML format (GraphXml) IList[Node] Nodes = new List(); GenerateNodes(Nodes, 3, 3); SimpleFlowDiagramGeneratorCompatibleGraphRender XmlConverter = new SimpleFlowDiagramGeneratorCompatibleGraphRender(); string GraphXml = XmlConverter.RenderGraph(Nodes); string GraphXmlFilePath = "GraphXml.xml"; System.IO.File.WriteAllText(GraphXmlFilePath, GraphXml); Console.WriteLine("Finished writing graph to XML format compatible with SimpleFlowDiagramGenerator.exe"); // STEP 2. Read back from GraphXml MemoryStream Stream = new MemoryStream(); StreamWriter writer = new StreamWriter(Stream); writer.Write(GraphXml); writer.Flush(); Stream.Position = 0; System.Xml.XmlReader XmlRdr = System.Xml.XmlReader.Create(Stream); IList[Node] ResurrectedNodes = XmlConverter.ReadGraphXml(XmlRdr); DiagramCanvasEngine.GenerateLayout( ResurrectedNodes, Node.DEFAULT_NODE_HEIGHT / 2, CanvasDefinition.LayoutDirection.LeftToRight ); // STEP 3. Render the nodes to HTML file - this is exactly what "SimpleFlowDiagramGenerator.exe" does. It reads input xml file which defines the nodes. Then render flowchart to HTML file. IGraphRender Html5Render = new Html5GraphRender(); Html5Render.RenderGraph(Canvas, ResurrectedNodes, DisplaySettings, "Flowchart.html"); Console.WriteLine("Finished render to HTML5 to Flowchart.html"); return; }
public static void GenerateNodes( IList Nodes, int NumRootNodes, int MaxTreeDepth ) { Node RootNode; for (int i = 0; i < NumRootNodes; i++) { RootNode = new Node(); RootNode.NodeHeader = "Root_" + i; RootNode.NodeDetail = "Some detail ..."; RootNode.NodeHyperLink = ""; RootNode.Depth = 0; Nodes.Add(RootNode); GenerateSingleGraph(Nodes, RootNode, MaxTreeDepth); } return; } public static void GenerateSingleGraph( IList Nodes, Node RootNode, int MaxTreeDepth ) { int CurrentDepth = 0; RecursiveGenerateGraph(Nodes, RootNode, MaxTreeDepth, ref CurrentDepth); return; } public static void RecursiveGenerateGraph( IList Nodes, Node Node, int MaxTreeDepth, ref int CurrentDepth ) { CurrentDepth++; Random rnd = new Random(DateTime.Now.Second); if (CurrentDepth < MaxTreeDepth) { int NumChildren = rnd.Next(5); for (int i = 0; i < NumChildren; i++) { Node Child = new Node(); Child.NodeHeader = Node.NodeHeader + "." + "Child_Level" + CurrentDepth + "_Num" + i; Child.NodeDetail = "Some detail ..."; Child.NodeHyperLink = ""; Child.Depth = CurrentDepth; Child.ParentNodes.Add(Node); Node.ChildNodes.Add(Child); Nodes.Add(Child); int CopyCurrentDepeth = CurrentDepth; RecursiveGenerateGraph(Nodes, Child, MaxTreeDepth, ref CopyCurrentDepeth); } } return; } } }

Happy Coding!

You may also want to check out how to convert DataTable to/from HTML Table –



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s