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, "http://graphml.graphdrawing.org/xmlns\nhttp://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"); for (int i = 0; i < Properties.Count; i++) { GraphMLProperty graphMLProperty = Properties[i]; graphMLProperty.Id = "d" + i; graphMLProperty.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(ArcFilter.All).ToString(CultureInfo.InvariantCulture)); xml.WriteAttributeString("parse.order", "nodesfirst"); DefinePropertyValues(xml, Graph); foreach (Node item in Graph.Nodes()) { xml.WriteStartElement("node", xmlns.NamespaceName); xml.WriteAttributeString("id", item.Id.ToString(CultureInfo.InvariantCulture)); DefinePropertyValues(xml, item); xml.WriteEndElement(); } foreach (Arc item2 in Graph.Arcs(ArcFilter.All)) { xml.WriteStartElement("edge", xmlns.NamespaceName); xml.WriteAttributeString("id", item2.Id.ToString(CultureInfo.InvariantCulture)); if (Graph.IsEdge(item2)) { xml.WriteAttributeString("directed", "false"); } xml.WriteAttributeString("source", Graph.U(item2).Id.ToString(CultureInfo.InvariantCulture)); xml.WriteAttributeString("target", Graph.V(item2).Id.ToString(CultureInfo.InvariantCulture)); DefinePropertyValues(xml, item2); xml.WriteEndElement(); } xml.WriteEndElement(); xml.WriteEndElement(); }
/// Loads from an XML document. public void Load(XDocument doc) { // Namespaces are ignored so we can load broken documents. if (Graph == null) { Graph = new CustomGraph(); } IBuildableGraph buildableGraph = (IBuildableGraph)Graph; buildableGraph.Clear(); XElement xGraphML = doc.Root; // load properties Properties.Clear(); Dictionary <string, GraphMLProperty> propertyById = new Dictionary <string, GraphMLProperty>(); foreach (var xKey in Utils.ElementsLocal(xGraphML, "key")) { foreach (var handler in PropertyLoaders) { try { GraphMLProperty p = handler(xKey); Properties.Add(p); propertyById[p.Id] = p; break; } catch (ArgumentException) { } } } // load graph XElement xGraph = Utils.ElementLocal(xGraphML, "graph"); Directedness defaultDirectedness = (xGraph.Attribute("edgedefault").Value == "directed" ? Directedness.Directed : Directedness.Undirected); ReadProperties(propertyById, xGraph, Graph); // load nodes Dictionary <string, Node> nodeById = new Dictionary <string, Node>(); foreach (var xNode in Utils.ElementsLocal(xGraph, "node")) { Node node = buildableGraph.AddNode(); nodeById[xNode.Attribute("id").Value] = node; ReadProperties(propertyById, xNode, node); } // load arcs foreach (var xArc in Utils.ElementsLocal(xGraph, "edge")) { Node u = nodeById[xArc.Attribute("source").Value]; Node v = nodeById[xArc.Attribute("target").Value]; Directedness dir = defaultDirectedness; XAttribute dirAttr = xArc.Attribute("directed"); if (dirAttr != null) { dir = (dirAttr.Value == "true" ? Directedness.Directed : Directedness.Undirected); } Arc arc = buildableGraph.AddArc(u, v, dir); ReadProperties(propertyById, xArc, arc); } }
public void Load(XDocument doc) { if (Graph == null) { Graph = new CustomGraph(); } IBuildableGraph buildableGraph = (IBuildableGraph)Graph; buildableGraph.Clear(); XElement root = doc.Root; Properties.Clear(); Dictionary <string, GraphMLProperty> dictionary = new Dictionary <string, GraphMLProperty>(); foreach (XElement item in Utils.ElementsLocal(root, "key")) { using (List <Func <XElement, GraphMLProperty> > .Enumerator enumerator2 = PropertyLoaders.GetEnumerator()) { while (true) { if (enumerator2.MoveNext()) { Func <XElement, GraphMLProperty> current2 = enumerator2.Current; try { GraphMLProperty graphMLProperty = current2(item); Properties.Add(graphMLProperty); dictionary[graphMLProperty.Id] = graphMLProperty; } catch (ArgumentException) { continue; } } break; } } } XElement xElement = Utils.ElementLocal(root, "graph"); Directedness directedness = (!(xElement.Attribute("edgedefault").Value == "directed")) ? Directedness.Undirected : Directedness.Directed; ReadProperties(dictionary, xElement, Graph); Dictionary <string, Node> dictionary2 = new Dictionary <string, Node>(); foreach (XElement item2 in Utils.ElementsLocal(xElement, "node")) { Node node = buildableGraph.AddNode(); dictionary2[item2.Attribute("id").Value] = node; ReadProperties(dictionary, item2, node); } foreach (XElement item3 in Utils.ElementsLocal(xElement, "edge")) { Node u = dictionary2[item3.Attribute("source").Value]; Node v = dictionary2[item3.Attribute("target").Value]; Directedness directedness2 = directedness; XAttribute xAttribute = item3.Attribute("directed"); if (xAttribute != null) { directedness2 = ((!(xAttribute.Value == "true")) ? Directedness.Undirected : Directedness.Directed); } Arc arc = buildableGraph.AddArc(u, v, directedness2); ReadProperties(dictionary, item3, arc); } }