public void LoadXmlAttributes(IGraphElement o, XmlElement r) { foreach (var p in o.GetType().GetProperties().ToList()) { if (p.GetCustomAttributes().OfType <DataMemberAttribute>().Any()) { if (typeof(IList).IsAssignableFrom(p.PropertyType)) { var list = (IList)p.GetValue(o); foreach (var e in r.ChildNodes.OfType <XmlElement>().Where(n => n.LocalName == p.Name)) { LoadXmlCollection(o, e, list); } continue; } if (!p.CanWrite) { continue; } try { var attribute = r.Attributes.OfType <XmlAttribute>().FirstOrDefault(a => a.LocalName == p.Name); if (attribute == null) { continue; } var v = attribute.Value;//r.GetAttribute(p.Name); if (p.PropertyType == typeof(double)) { p.SetValue(o, double.Parse(v, CultureInfo.InvariantCulture)); } if (p.PropertyType == typeof(int)) { p.SetValue(o, int.Parse(v, CultureInfo.InvariantCulture)); } if (p.PropertyType == typeof(string)) { p.SetValue(o, v); } } catch (ArgumentException) { } } } }
//Stolen from GXLExport.cs private string CanonizeElement (IGraphElement elem) { List<String> attributes = new List<String> (); foreach (AttributeType attrType in elem.Type.AttributeTypes) { object value = elem.GetAttribute (attrType.Name); String valType; String valuestr = (value == null) ? "" : value.ToString (); switch (attrType.Kind) { case AttributeKind.BooleanAttr: valType = "bool"; valuestr = ((bool)value) ? "true" : "false"; break; case AttributeKind.DoubleAttr: case AttributeKind.FloatAttr: valType = "double"; valuestr = ((double)value).ToString(System.Globalization.CultureInfo.InvariantCulture); break; case AttributeKind.ByteAttr: case AttributeKind.ShortAttr: case AttributeKind.IntegerAttr: case AttributeKind.LongAttr: valType = "int"; break; // TODO: This does not allow differentiating between empty and null strings case AttributeKind.StringAttr: valType = "string"; valuestr= "\"" + valuestr + "\""; break; case AttributeKind.EnumAttr: valType = "enum"; break; default: throw new Exception ("Unsupported attribute value type: \"" + attrType.Kind + "\""); } attributes.Add (attrType.Name + ":" + valType + ":" + valuestr); } attributes.Sort ((x,y) => x.CompareTo (y)); string elemTypeName = null; string elemClassName = elem.GetType ().Name; if (elem is INode) { elemTypeName = "Node"; } else if (elem is IEdge) { elemTypeName = "Edge"; } else { elemTypeName = "Unsupported Graph Element Type"; } StringBuilder sb = new StringBuilder (elemClassName + ":" + elemTypeName); if (attributes.Count > 0) { sb.Append (","); for (int i=0; i< (attributes.Count-1); i++) { sb.Append (attributes [i] + ","); } } if(attributes.Count > 0) sb.Append(attributes[attributes.Count-1]); return sb.ToString(); }
//Stolen from GXLExport.cs private string CanonizeElement(IGraphElement elem) { List <String> attributes = new List <String>(); foreach (AttributeType attrType in elem.Type.AttributeTypes) { object value = elem.GetAttribute(attrType.Name); String valType; String valuestr = (value == null) ? "" : value.ToString(); switch (attrType.Kind) { case AttributeKind.BooleanAttr: valType = "bool"; valuestr = ((bool)value) ? "true" : "false"; break; case AttributeKind.DoubleAttr: case AttributeKind.FloatAttr: valType = "double"; valuestr = ((double)value).ToString(System.Globalization.CultureInfo.InvariantCulture); break; case AttributeKind.ByteAttr: case AttributeKind.ShortAttr: case AttributeKind.IntegerAttr: case AttributeKind.LongAttr: valType = "int"; break; // TODO: This does not allow differentiating between empty and null strings case AttributeKind.StringAttr: valType = "string"; valuestr = "\"" + valuestr + "\""; break; case AttributeKind.EnumAttr: valType = "enum"; break; default: throw new Exception("Unsupported attribute value type: \"" + attrType.Kind + "\""); } attributes.Add(attrType.Name + ":" + valType + ":" + valuestr); } attributes.Sort((x, y) => x.CompareTo(y)); string elemTypeName = null; string elemClassName = elem.GetType().Name; if (elem is INode) { elemTypeName = "Node"; } else if (elem is IEdge) { elemTypeName = "Edge"; } else { elemTypeName = "Unsupported Graph Element Type"; } StringBuilder sb = new StringBuilder(elemClassName + ":" + elemTypeName); if (attributes.Count > 0) { sb.Append(","); for (int i = 0; i < (attributes.Count - 1); ++i) { sb.Append(attributes [i] + ","); } } if (attributes.Count > 0) { sb.Append(attributes[attributes.Count - 1]); } return(sb.ToString()); }