开发者

.NET XmlSerializer fails with List<T>

I'm using a singleton class to save all my settings info. It's first utilized by calling Settings.ValidateSettings(@"C:\MyApp").

The problem I'm having i开发者_JAVA百科s that 'List Contacts' is causing the xmlserializer to fail to write the settings file, or to load said settings. If I comment out the List<T> then I have no problems saving/loading the xml file. What am I doing wrong?

// The actual settings to save  

public class MyAppSettings
{
    public bool FirstLoad
    { get; set; }

    public string VehicleFolderName
    { get; set; }

    public string ContactFolderName
    { get; set; }

    public List<ContactInfo> Contacts
    {
        get
        {
            if (contacts == null)
                contacts = new List<ContactInfo>();
            return contacts;
        }
        set
        {
            contacts = value;
        }
    }

    private List<ContactInfo> contacts;
}

// The class in which the settings are manipulated
public static class Settings
{
    public static string SettingPath;
    private static MyAppSettings instance;
    public static MyAppSettings Instance
    {
        get
        {
            if (instance == null)
                instance = new MyAppSettings();
            return instance;
        }
        set
        {
            instance = value;
        }
    }

    public static void InitializeSettings(string path)
    {
        SettingPath = Path.GetFullPath(path + "\\MyApp.xml");
        if (File.Exists(SettingPath))
        {
            LoadSettings();
        }
        else
        {
            Instance.FirstLoad = true;
            Instance.VehicleFolderName = "Cars";
            Instance.ContactFolderName = "Contacts";
            SaveSettingsFile();
        }
    }

    // load the settings from the xml file
    private static void LoadSettings()
    {
        XmlSerializer ser = new XmlSerializer(typeof(MyAppSettings));

        TextReader reader = new StreamReader(SettingPath);
        Instance = (MyAppSettings)ser.Deserialize(reader);
        reader.Close();
    }

    // Save the settings to the xml file
    public static void SaveSettingsFile()
    {
        XmlSerializer ser = new XmlSerializer(typeof(MyAppSettings));

        TextWriter writer = new StreamWriter(SettingPath);
        ser.Serialize(writer, Settings.Instance);
        writer.Close();

    }

    public static bool ValidateSettings(string initialFolder)
    {
        try
        {
            Settings.InitializeSettings(initialFolder);
        }
        catch (Exception e)
        {
             return false;
        }

        // Do some validation logic here

        return true;
    }
}

// A utility class to contain each contact detail
public class ContactInfo
{
    public string ContactID;
    public string Name;
    public string PhoneNumber;
    public string Details;
    public bool Active;
    public int SortOrder;

}


@itowlson After your reply I loaded the code file into a new forms application and it worked. The project I'm working on though is actually a plugin of another program. So it's due to things that are out of my control that are causing the serializer to fail with List

Thanks for replying that it worked for you, which helped me to realize what's happening

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜