GraphMLWriter.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7. namespace DrawGraph
  8. {
  9. public class GraphMLWriter
  10. {
  11. public static void WriteEdges(string path, Edge[] edges, bool direction)
  12. {
  13. //XmlDocument doc = new XmlDocument();
  14. //XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  15. //var root = doc.CreateElement("graph");
  16. //var attr = doc.CreateAttribute("id");
  17. //attr.InnerText = "G";
  18. //root.Attributes.Append(attr);
  19. //var dir = doc.CreateAttribute("edgedefault");
  20. //dir.InnerText = direction ? "directed" : "undirected";
  21. //root.Attributes.Append(dir);
  22. //doc.AppendChild(declaration);
  23. //doc.AppendChild(root);
  24. //XmlProcessingInstruction pi =
  25. // doc.CreateProcessingInstruction("graphml", "xmlns=\"http://graphml.graphdrawing.org/xmlns \" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance \" xsi: schemaLocation = \"http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd \"");
  26. //doc.InsertBefore(pi, doc.ChildNodes[1]);
  27. //for (int i = 0; i < edges.Length; i++)
  28. //{
  29. // XmlNode edge = doc.CreateElement("edge");
  30. // var sourceAttribute = doc.CreateAttribute("source");
  31. // for (int j = 0; j < nodes.Length; j++)
  32. // if (Node.Compare(edges[i].SourceNode, nodes[j]))
  33. // sourceAttribute.InnerText = "n" + j.ToString();
  34. // edge.Attributes.Append(sourceAttribute);
  35. // var targetAttribute = doc.CreateAttribute("target");
  36. // for (int j = 0; j < nodes.Length; j++)
  37. // if (Node.Compare(edges[i].TargetNode, nodes[j]))
  38. // targetAttribute.InnerText = "n" + j.ToString();
  39. // edge.Attributes.Append(targetAttribute);
  40. // if (edges[i].Weight > 0)
  41. // {
  42. // var weightAttr = doc.CreateAttribute("weight");
  43. // weightAttr.InnerText = edges[i].Weight.ToString();
  44. // edge.Attributes.Append(weightAttr);
  45. // }
  46. // root.AppendChild(edge);
  47. //}
  48. //doc.Save(path);
  49. }
  50. public static void WriteAllGraph(string path, Node[] nodes, Edge[] edges, bool direction)
  51. {
  52. XmlDocument doc = new XmlDocument();
  53. XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  54. var root = doc.CreateElement("graph");
  55. var attr = doc.CreateAttribute("id");
  56. attr.InnerText = "G";
  57. root.Attributes.Append(attr);
  58. var dir = doc.CreateAttribute("edgedefault");
  59. dir.InnerText = direction ? "directed" : "undirected";
  60. root.Attributes.Append(dir);
  61. doc.AppendChild(declaration);
  62. doc.AppendChild(root);
  63. XmlProcessingInstruction pi =
  64. doc.CreateProcessingInstruction("graphml", "xmlns=\"http://graphml.graphdrawing.org/xmlns \" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance \" xsi: schemaLocation = \"http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd \"");
  65. doc.InsertBefore(pi, doc.ChildNodes[1]);
  66. for (int i = 0; i < nodes.Length; i++)
  67. {
  68. XmlNode node = doc.CreateElement("node");
  69. var attribute = doc.CreateAttribute("id");
  70. attribute.InnerText = "n" + i.ToString();
  71. node.Attributes.Append(attribute);
  72. var XCoord = doc.CreateAttribute("positionX");
  73. XCoord.InnerText = nodes[i].X.ToString();
  74. node.Attributes.Append(XCoord);
  75. var YCoord = doc.CreateAttribute("positionY");
  76. YCoord.InnerText = nodes[i].Y.ToString();
  77. node.Attributes.Append(YCoord);
  78. root.AppendChild(node);
  79. }
  80. for (int i = 0; i < edges.Length; i++)
  81. {
  82. XmlNode edge = doc.CreateElement("edge");
  83. var sourceAttribute = doc.CreateAttribute("source");
  84. for (int j = 0; j < nodes.Length; j++)
  85. if (Node.Compare(edges[i].SourceNode, nodes[j]))
  86. sourceAttribute.InnerText = "n" + j.ToString();
  87. edge.Attributes.Append(sourceAttribute);
  88. var targetAttribute = doc.CreateAttribute("target");
  89. for (int j = 0; j < nodes.Length; j++)
  90. if (Node.Compare(edges[i].TargetNode, nodes[j]))
  91. targetAttribute.InnerText = "n" + j.ToString();
  92. edge.Attributes.Append(targetAttribute);
  93. if (edges[i].Weight > 0)
  94. {
  95. var weightAttr = doc.CreateAttribute("weight");
  96. weightAttr.InnerText = edges[i].Weight.ToString();
  97. edge.Attributes.Append(weightAttr);
  98. }
  99. root.AppendChild(edge);
  100. }
  101. doc.Save(path);
  102. }
  103. }
  104. }