Reputation: 1870
I'd like to Deserialize my "DataStore" to get a list of Typs. First i want to make theese in XMl with the XMLSerializer but it seems that he dont like Interfaces, Abstract Class and Typs ... but there is no Workaround so i need to store my Main content in an XML class:
public class InstalledObjects
{
private InstalledObjects()
{
}
static InstalledObjects _instance = new InstalledObjects();
ObservableCollection<AbstrICTSUseObject> _installedObects = new ObservableCollection<AbstrICTSUseObject>();
public static InstalledObjects Instance
{
get { return _instance; }
set { _instance = value; }
}
public ObservableCollection<AbstrICTSUseObject> InstalledObects
{
get { return _installedObects; }
set { _installedObects = value; }
}
public void Saves()
{
List<Type> types = new List<Type>();
foreach (var item in InstalledObects)
{
types.Add( item.GetType() );
}
TypeStore ts = new TypeStore();
ts.Typen = types;
ts.SaveAsBinary("TypeStore.xml");
this.SaveAsXML("LocalDataStore.xml", types.ToArray());
}
public void Load()
{
if (File.Exists("LocalDataStore.xml"))
{
TypeStore ts = new TypeStore();
ts.LoadFromBinary("LocalDataStore.xml");
this.LoadFromXML("LocalDataStore.xml",ts.Typen.ToArray());
}
}
}
And store my Typs in an Simple class:
[Serializable]
public class TypeStore
{
List<Type> _typen = new List<Type>();
public List<Type> Typen
{
get { return _typen; }
set { _typen = value; }
}
}
Ok good think this works as long as i just Save all, and i Think this will also working if there would not the litte problem that the "LoadFromBinary" throw some expetions -.-
public static void SaveAsBinary(this Object A, string FileName)
{
FileStream fs = new FileStream(FileName, FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, A);
}
public static void LoadFromBinary(this Object A, string FileName)
{
if (File.Exists(FileName))
{
Stream fs = new FileStream(FileName, FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
A = formatter.Deserialize(fs) ;
}
}
The Expeption:
The input stream is not a valid binary format. The starting contents (in bytes) are: 3C-3F-78-6D-6C-20-76-65-72-73-69-6F-6E-3D-22-31-2E ...
Thx for help Venson :-)
Upvotes: 0
Views: 2410
Reputation: 1062780
Is this as simple as the fact that you're reading from the wrong file?
Note:
ts.SaveAsBinary("TypeStore.xml");
this.SaveAsXML("LocalDataStore.xml", types.ToArray());
Then:
ts.LoadFromBinary("LocalDataStore.xml");
this.LoadFromXML("LocalDataStore.xml", ts.Typen.ToArray());
Should be:
ts.LoadFromBinary("TypeStore.xml");
this.LoadFromXML("LocalDataStore.xml", ts.Typen.ToArray());
however, note that calling it .xml is misleading. Also: watch out for versioning - BinaryFormatter
is a real pig for that. Personally, I'd just be manually serializing each type's AssemblyQualifiedName
- which can be done in normal xml.
Upvotes: 1