Skip to content

Commit

Permalink
Fix read and write of generic XML
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraser Greenroyd committed Sep 11, 2023
1 parent 6470096 commit 699a2e0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion XML_Adapter/CRUD/Default/CreateDefault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ private bool CreateDefault<T>(IEnumerable<T> objects, XMLConfig config)
foreach (System.Reflection.PropertyInfo pi in bhomProperties)
overrides.Add(typeof(BHoMObject), pi.Name, new XmlAttributes { XmlIgnore = true });

var exportType = typeof(T);
if (exportType == typeof(IBHoMObject))
exportType = objects.First().GetType();

XmlSerializerNamespaces xns = new XmlSerializerNamespaces();
XmlSerializer szer = new XmlSerializer(typeof(T), overrides);
XmlSerializer szer = new XmlSerializer(exportType, overrides);
TextWriter ms = new StreamWriter(_fileSettings.GetFullFileName());
foreach (var obj in objects)
{
Expand Down
8 changes: 7 additions & 1 deletion XML_Adapter/CRUD/Default/ReadDefault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ private IEnumerable<IBHoMObject> ReadDefault(Type type = null, XMLConfig config
object obj = null;
try
{
System.Reflection.PropertyInfo[] bhomProperties = typeof(BHoMObject).GetProperties();
XmlAttributeOverrides overrides = new XmlAttributeOverrides();

foreach (System.Reflection.PropertyInfo pi in bhomProperties)
overrides.Add(typeof(BHoMObject), pi.Name, new XmlAttributes { XmlIgnore = true });

TextReader reader = new StreamReader(_fileSettings.GetFullFileName());
XmlSerializer szer = new XmlSerializer(type);
XmlSerializer szer = new XmlSerializer(type, overrides);
obj = System.Convert.ChangeType(szer.Deserialize(reader), type);
reader.Close();
}
Expand Down

0 comments on commit 699a2e0

Please sign in to comment.