private static void DefineXmlSerializer(Type ty) { XmlAttributes attrs = new XmlAttributes(); foreach (Type t in s_loadedCompTypes) { XmlArrayItemAttribute attr = new XmlArrayItemAttribute { ElementName = t.Name, Type = t }; attrs.XmlArrayItems.Add(attr); } foreach (Type t in s_pluginCompTypes) { XmlArrayItemAttribute attr = new XmlArrayItemAttribute { ElementName = t.Name, Type = t }; attrs.XmlArrayItems.Add(attr); } XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); attrOverrides.Add(typeof(CompBase), "ChildArray", attrs); MCoreXmlSerializer.DefineSerializer(ty, attrOverrides); }
/// <summary> /// recursively Export (saveAs) all the settings of the CompBase component /// </summary> /// <param name="filePath"></param> /// <param name="compChild"></param> public static void ExportSettings(string filePath, CompBase compChild) { try { U.EnsureWritable(filePath); DefineXmlSerializer(compChild.GetType()); MCoreXmlSerializer.SaveObjectToFile(filePath, compChild); } catch (Exception ex) { U.LogPopup(ex, "Error Saving '{0}'", compChild.Name); } }
/// <summary> /// Read the xml file and create a Component tree /// </summary> /// <param name="ty"></param> /// <param name="filePath"></param> /// <returns></returns> public static CompBase ImportFile(Type ty, string filePath) { DefineXmlSerializer(ty); // Try to deserialize it return(MCoreXmlSerializer.LoadObjectFromFile(filePath, ty) as CompBase); }