123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml;
- namespace DrawGraph
- {
- public class GraphMLWriter
- {
- public static void WriteEdges(string path, Edge[] edges, bool direction)
- {
- //XmlDocument doc = new XmlDocument();
- //XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
- //var root = doc.CreateElement("graph");
- //var attr = doc.CreateAttribute("id");
- //attr.InnerText = "G";
- //root.Attributes.Append(attr);
- //var dir = doc.CreateAttribute("edgedefault");
- //dir.InnerText = direction ? "directed" : "undirected";
- //root.Attributes.Append(dir);
- //doc.AppendChild(declaration);
- //doc.AppendChild(root);
- //XmlProcessingInstruction pi =
- // 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 \"");
- //doc.InsertBefore(pi, doc.ChildNodes[1]);
- //for (int i = 0; i < edges.Length; i++)
- //{
- // XmlNode edge = doc.CreateElement("edge");
- // var sourceAttribute = doc.CreateAttribute("source");
- // for (int j = 0; j < nodes.Length; j++)
- // if (Node.Compare(edges[i].SourceNode, nodes[j]))
- // sourceAttribute.InnerText = "n" + j.ToString();
- // edge.Attributes.Append(sourceAttribute);
- // var targetAttribute = doc.CreateAttribute("target");
- // for (int j = 0; j < nodes.Length; j++)
- // if (Node.Compare(edges[i].TargetNode, nodes[j]))
- // targetAttribute.InnerText = "n" + j.ToString();
- // edge.Attributes.Append(targetAttribute);
- // if (edges[i].Weight > 0)
- // {
- // var weightAttr = doc.CreateAttribute("weight");
- // weightAttr.InnerText = edges[i].Weight.ToString();
- // edge.Attributes.Append(weightAttr);
- // }
- // root.AppendChild(edge);
- //}
- //doc.Save(path);
- }
- public static void WriteAllGraph(string path, Node[] nodes, Edge[] edges, bool direction)
- {
- XmlDocument doc = new XmlDocument();
- XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
- var root = doc.CreateElement("graph");
- var attr = doc.CreateAttribute("id");
- attr.InnerText = "G";
- root.Attributes.Append(attr);
- var dir = doc.CreateAttribute("edgedefault");
- dir.InnerText = direction ? "directed" : "undirected";
- root.Attributes.Append(dir);
- doc.AppendChild(declaration);
- doc.AppendChild(root);
- XmlProcessingInstruction pi =
- 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 \"");
- doc.InsertBefore(pi, doc.ChildNodes[1]);
- for (int i = 0; i < nodes.Length; i++)
- {
- XmlNode node = doc.CreateElement("node");
- var attribute = doc.CreateAttribute("id");
- attribute.InnerText = "n" + i.ToString();
- node.Attributes.Append(attribute);
- var XCoord = doc.CreateAttribute("positionX");
- XCoord.InnerText = nodes[i].X.ToString();
- node.Attributes.Append(XCoord);
- var YCoord = doc.CreateAttribute("positionY");
- YCoord.InnerText = nodes[i].Y.ToString();
- node.Attributes.Append(YCoord);
- root.AppendChild(node);
- }
- for (int i = 0; i < edges.Length; i++)
- {
- XmlNode edge = doc.CreateElement("edge");
- var sourceAttribute = doc.CreateAttribute("source");
- for (int j = 0; j < nodes.Length; j++)
- if (Node.Compare(edges[i].SourceNode, nodes[j]))
- sourceAttribute.InnerText = "n" + j.ToString();
- edge.Attributes.Append(sourceAttribute);
- var targetAttribute = doc.CreateAttribute("target");
- for (int j = 0; j < nodes.Length; j++)
- if (Node.Compare(edges[i].TargetNode, nodes[j]))
- targetAttribute.InnerText = "n" + j.ToString();
- edge.Attributes.Append(targetAttribute);
- if (edges[i].Weight > 0)
- {
- var weightAttr = doc.CreateAttribute("weight");
- weightAttr.InnerText = edges[i].Weight.ToString();
- edge.Attributes.Append(weightAttr);
- }
- root.AppendChild(edge);
- }
- doc.Save(path);
- }
- }
- }
|