I have a class that handles XMLs serialization.
public class DB
{
public List<Connection> lstConnections { get; set; }
static string sRootAttribute = "Connections";
public static DB LoadFromFile(string path)
{
FileStream fs = null;
DB db = null;
try
{
fs = File.Open(path, FileMode.Open);
var serializer = new XmlSerializer(typeof(DB), new XmlRootAttribute(sRootAttribute));
db = (DB)serializer.Deserialize(fs);
}
catch
{
}
if (fs != null)
{
fs.Close();
}
return db;
}
public static void SaveToFile(string path, object objData)
{
var fs = File.Open(path, FileMode.Create);
var serializer = new XmlSerializer(objData.GetType(), new XmlRootAttribute(sRootAttribute));
try
{
serializer.Serialize(fs, objData);
}
catch
{
}
fs.Close();
}
}
The LoadFromFile
method is somewhat generic, because it doesn't consider the serialized data type.
However, SaveToFile
does GetType
on the object that is being serialized.
Therefore, when I'm trying to deserialize the file with LoadFromFile
I'm having problems. I'm not getting any exceptions, but the lstConnections
is empty.
If you have ownership over the Connection class, then: