Reputation: 402
my XML-Serialization is not working.
The following code throws an exception:
XmlSerializer x = new XmlSerializer(typeof(GeneralSettings));
I guess something is wrong with my GeneralSettings class? I can't figure out what the problem is exactly.
Class which shall be serialized:
[Serializable()]
class GeneralSettings
{
// ---------------------------------------------------------------//
#region Properties for settings
// ---------------------------------------------------------------//
public string ActiveLanguage { get; set; }
public string ActiveLeague { get; set; }
// ---------------------------------------------------------------//
#endregion
// ---------------------------------------------------------------//
// ---------------------------------------------------------------//
#region Constructors
// ---------------------------------------------------------------//
public GeneralSettings()
{
this.ActiveLanguage = "English";
this.ActiveLeague = "";
}
// ---------------------------------------------------------------//
#endregion
// ---------------------------------------------------------------//
}
My BaseSettings Class to Serialize and deserialize
class BaseSettings
{
protected static string FileName
{
get
{
return Path.Combine(Environment.CurrentDirectory, @"Settings\XML\GeneralSettings.xml");
}
}
public static GeneralSettings Load()
{
using (var stream = new FileStream(FileName, FileMode.Open))
{
return (GeneralSettings)new XmlSerializer(typeof(GeneralSettings)).Deserialize(stream);
}
}
public static void Save(GeneralSettings settings)
{
using (var stream = new FileStream(FileName, FileMode.Open))
{
XmlSerializer x = new XmlSerializer(typeof(GeneralSettings));
x.Serialize(stream, settings);
}
}
Thanks in advance for helping me!
Upvotes: 0
Views: 610
Reputation: 27871
The class has to be public in order to be serializable via the XmlSerializer
class.
Change the definition of the class to this:
public class GeneralSettings
{
...
}
Upvotes: 3