FubarDevelopment / QuickGraph

Fork of https://quickgraph.codeplex.com/
Microsoft Public License
9 stars 2 forks source link

CP-14539: yEd files deserialization #94

Open fubar-coder opened 6 years ago

fubar-coder commented 6 years ago

From unknown CodePlex user on Tuesday, 27 October 2009 06:53:41

Hi,  

  First of all I'd like to say that QuickGraph is a impressive piece of work. Congratuations!   I'm looking for a way to read graphml files generated by yEd, a free graph editor. www.yworks.com/products/yed/   I can't easily figure out how to customize the Deserializer to read such Nodes, I get a not very meaningful error when trying to deserialize a graph (see xml below) with this code  

      var g = new AdjacencyGraph<string, Edge>(); using (var xreader = XmlReader.Create("z:/test.graphml")) { g.DeserializeFromGraphML(xreader, id => id, (source, target, id) => new Edge(source, target) ); }             <?xml version="1.0" encoding="UTF-8" standalone="no"?>

TICK SMA SMA2

        What would be the minimum steps to read succesfully the graph, without data attached, and possibly with the data nodes as well?   Thanks for your help!   Alexandre

fubar-coder commented 6 years ago

Form unknown CodePlex user on Tuesday, 01 February 2011 07:40:26

Hi, Alexandre -- I am not sure what your error is, but I solved some Namespace difficulties when reading such files with Linq-to-XML. Here is sample code that works over yEd graphml files

var graph = new AdjacencyGraph<string, Edge>(true);

var file = @". . . your file path here . . ."; var theDoc = XDocument.Load(file);

(from node in theDoc.Descendants() where node.Name.LocalName == "node" select graph.AddVertex(node.Attribute("id").Value)).Run();

var edges = (from edge in theDoc.Descendants() where edge.Name.LocalName == "edge" select new Edge( edge.Attribute("source").Value, edge.Attribute("target").Value));

edges.Select(graph.AddEdge).Run(); var edgeCost = new Dictionary<Edge, double>(); graph.Edges.Do(e => edgeCost.Add(e, 1)).Run();

var dijkstra = new DijkstraShortestPathAlgorithm<string, Edge>( graph, AlgorithmExtensions.GetIndexer<Edge, double>( edgeCost));

var predObsvr = new QuickGraph.Algorithms.Observers .VertexPredecessorRecorderObserver<string, Edge>(); predObsvr.Attach(dijkstra);

var distObsvr = new VertexDistanceRecorderObserver<string, Edge>( AlgorithmExtensions.GetIndexer<Edge, double>(edgeCost)); distObsvr.Attach(dijkstra);