tags:

views:

1348

answers:

4

I have the following class:

[Serializable]
[XmlRoot ( ElementName = "TextData", IsNullable = false)]
public class TextData
{
    private System.Drawing.Font fontColor;

    [XmlAttribute ( AttributeName = "Font" )]
    public System.Drawing.Font Font { get; set; }

    [XmlAttribute ( AttributeName = "FontColor" )]
    public System.Drawing.Color FontColor { get; set; }

    [XmlAttribute ( AttributeName = "Text" )]
    public string Text { get; set; }

    public TextData ( )
    {
    } // End of TextData
} // End of TextData

And Im attempting to save it with the following code:

    // Create our font dialog
    FontDialog fontDialog = new FontDialog ( );
    fontDialog.ShowColor = true;

    // Display the dialog and check for an ok
    if ( DialogResult.OK == fontDialog.ShowDialog ( ) )
    {
        // Save our changes for the font settings
        if ( null == Properties.Settings.Default.MainHeadlineTextData )
        {
            Properties.Settings.Default.MainHeadlineTextData = new TextData ( );
        }
        Properties.Settings.Default.MainHeadlineTextData.Font = fontDialog.Font;
        Properties.Settings.Default.MainHeadlineTextData.FontColor = fontDialog.Color;
        Properties.Settings.Default.Save ( );
    }

Everytime I load the the application, the Properties.Settings.Default.MainHeadlineTextData is still null. Saving does not seem to take effect. I read on another post that the class must be public and it is. Any ideas why this would not be working properly?

+1  A: 
  • Note, that if you do a full recompile the application get a new version internally so it is looking in another folder for the settings - and will of course find none and create a new default one.
  • When you save the data, no exceptio is thrown?
  • If you insert a breakpoint into the TextData constructor, is it called on application load?
codymanix
+3  A: 

It doesn't work because Color and Font are not xml-serializable. Even more than that, Font does not have parameterless constructor so Settings.Save() fails to serialize your object and doesn't save anything.

Unfortunately, by default infrastructure under class SettingsBase (from which your Settings class is inherited - see auto-generated code in Settings.Designer.cs file) doesn't throw exception on serialization error so you can't see the problem.

Remove Font and Color properties from your class and try again.

Another solution

Another solution is to use binary serialization. Open Settins.Designer.cs file and add attribute [SettingsSerializeAs(SettingsSerializeAs.Binary)] to the property Settings.MainHeadlineTextData.

Konstantin Spirin
Thank you. With your help I added [XmlIgnore] to the Font and Color options and used some serialization code from: http://devblog.antongochev.net/2008/06/26/serializedeserialize-a-font-tofrom-xml/ to get it working.
Zenox
A: 

All the variables in the custom class should be public

Ali
There's more to it than that.
Vidar
+1  A: 

To save classes to the application settings config file, the class must either support a TypeConverter or be XMLSerializable.

Your code does not implement a TypeConverter for your custom class, and the Color and Font classes are not XMLSerialiable.

How to implement a type converter: http://msdn.microsoft.com/en-us/library/ayybcxe5%28VS.80%29.aspx

tbergelt