/// Saves to an XML writer. private void Save(XmlWriter xml) { xml.WriteStartDocument(); xml.WriteStartElement("graphml", xmlns.NamespaceName); xml.WriteAttributeString("xmlns", "xsi", null, xmlnsXsi.NamespaceName); xml.WriteAttributeString("xmlns", "y", null, xmlnsY.NamespaceName); xml.WriteAttributeString("xmlns", "yed", null, xmlnsYed.NamespaceName); xml.WriteAttributeString("xsi", "schemaLocation", null, xsiSchemaLocation); for (int i = 0; i < Properties.Count; i++) { var p = Properties[i]; p.Id = "d" + i; p.GetKeyElement().WriteTo(xml); } xml.WriteStartElement("graph", xmlns.NamespaceName); xml.WriteAttributeString("id", "G"); xml.WriteAttributeString("edgedefault", "directed"); xml.WriteAttributeString("parse.nodes", Graph.NodeCount().ToString(CultureInfo.InvariantCulture)); xml.WriteAttributeString("parse.edges", Graph.ArcCount().ToString(CultureInfo.InvariantCulture)); xml.WriteAttributeString("parse.order", "nodesfirst"); DefinePropertyValues(xml, Graph); Dictionary <string, Node> nodeById = new Dictionary <string, Node>(); if (NodeId == null) { NodeId = new Dictionary <Node, string>(); } foreach (var kv in NodeId) { if (nodeById.ContainsKey(kv.Value)) { throw new Exception("Duplicate node id " + kv.Value); } nodeById[kv.Value] = kv.Key; } foreach (var node in Graph.Nodes()) { string id; NodeId.TryGetValue(node, out id); if (id == null) { id = node.Id.ToString(CultureInfo.InvariantCulture); while (nodeById.ContainsKey(id)) { id += '_'; } NodeId[node] = id; nodeById[id] = node; } xml.WriteStartElement("node", xmlns.NamespaceName); xml.WriteAttributeString("id", id); DefinePropertyValues(xml, node); xml.WriteEndElement(); // node } foreach (var arc in Graph.Arcs()) { string id; if (ArcId != null) { ArcId.TryGetValue(arc, out id); } else { id = null; } xml.WriteStartElement("edge", xmlns.NamespaceName); if (id != null) { xml.WriteAttributeString("id", id); } if (Graph.IsEdge(arc)) { xml.WriteAttributeString("directed", "false"); } xml.WriteAttributeString("source", NodeId[Graph.U(arc)]); xml.WriteAttributeString("target", NodeId[Graph.V(arc)]); DefinePropertyValues(xml, arc); xml.WriteEndElement(); // edge } xml.WriteEndElement(); // graph xml.WriteEndElement(); // graphml }