public static void CreateXML(IGWObject data, XmlWriter writer, LinkedList <IGWObject> buffer = null) { var ObjectType = data.GetType(); writer.WriteStartElement(ObjectType.Name); CreateXMLInner(data, writer, buffer); writer.WriteEndElement(); }
private static void CreateXMLInner(IGWObject data, XmlWriter writer, LinkedList <IGWObject> buffer = null) { buffer = buffer ?? new LinkedList <IGWObject>(); if (!buffer.Contains(data)) { buffer.Add(data); } var ObjectType = data.GetType(); //var interfaceprops = ObjectType.GetInterfaces().SelectMany(i => i.GetProperties()).ToList(); foreach (var prop in ObjectType.GetProperties())//.Union(interfaceprops)) { if (!prop.CanRead) { continue; } var value = prop.GetValue(data); if (value == null) { continue; } var proptype = prop.PropertyType; if (SimpleTypes.Contains(proptype)) { writer.WriteElementString(prop.Name, ConvertToString(proptype, value)); } else if (value is ILogic) { writer.WriteStartElement(prop.Name); writer.WriteAttributeString("MitId", (value as IGWObject).GWId.ToString()); CreateXMLInner(value as ILogic, writer, buffer); writer.WriteEndElement(); } else if (value is IGWContext) { continue; } else if (value is IGWObject) { if (buffer.Contains(value)) { writer.WriteStartElement(prop.Name); writer.WriteAttributeString("MitId", (value as IGWObject).GWId.ToString()); writer.WriteEndElement(); } else { writer.WriteStartElement(prop.Name); CreateXMLInner(prop.GetValue(data) as IGWObject, writer, buffer); writer.WriteEndElement(); } } else if (value is IEnumerable <IGWObject> ) { var elements = value as IEnumerable <IGWObject>; writer.WriteStartElement(prop.Name); foreach (var element in elements) { writer.WriteStartElement("Item"); writer.WriteAttributeString("MitId", (element).GWId.ToString()); if (!buffer.Contains(element)) { CreateXMLInner(element, writer, buffer); } writer.WriteEndElement(); } writer.WriteEndElement(); } } }