views:

1697

answers:

2

Here's the class im trying to store

 [Serializable]
[XmlRoot(ElementName = "Database", IsNullable = false, Namespace = "http://somesite.com")]

class Database
{
    [XmlAttribute(AttributeName = "Name")]   
    public string Name { get; set; }

    [XmlAttribute(AttributeName = "Provider")]
    public DatabaseProvider Provider { get; set; }

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

    [XmlElement("DatabaseEntry")]
    public List<DatabaseEntry> SavedEntries { get; set; }

    public Database()
    {
        SavedEntries = new List<DatabaseEntry>();
    }

    public Database(string type, string provider, string driver)
    {

        Name = type;
        Driver = driver;
        Provider = DatabaseProvider.SqlClient;

        Provider = SetProvider(provider);
        SavedEntries = new List<DatabaseEntry>();
    }

    private DatabaseProvider SetProvider(string provider)
    {
        switch (provider)
        {
            case "OleDb":
                return DatabaseProvider.OleDb;
            case "SqlClient":
                return DatabaseProvider.SqlClient;
            default:
                return DatabaseProvider.SqlClient;
        }
    }
}

[Serializable]
[XmlRoot(ElementName = "DatabaseEntry", IsNullable = false, Namespace = "http://somesite.com")]

class DatabaseEntry

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

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

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

    public DatabaseEntry()
    { }
}

Database is the parent class that I'm trying store, anyone see anything wrong? When I instantiate the class and save it, nothing gets saved. What gives?

EDIT! The code that is being used to save:

if (Settings.Default.SqlDatabase == null) 
   { 
      Settings.Default.SqlDatabase = CreateNewDatabase();
      Settings.Default.Save();
   }

private Database CreateNewDatabase()
{
    Database data = new Database("SQL Server", "SqlClient", "SQL Native Client");
    data.SavedEntries.Add(new DatabaseEntry()
    {
        Name = "Default",
        ConnectionString = @"Hello1",
        DsnString = @"Hello2"
    });
    return data;
}

I'm simply just trying to store an instance of a Database class into the AppSettings

+3  A: 

You could try making the classes public - XmlSerializer is fussy about that.

However, I seem to recall that the settings code is much more interested in type-converters; you could try writing a TypeConverter for it?

Marc Gravell
yep that did it, i didn't have them as public. Thanks alot bud!
Tom
Was it just the accessor that you had to change to Public on it's own or did you have to use TypeConverter as well?
Vidar
@Vidar - from Tom's comment, I assume just the `public` did it.
Marc Gravell
@Vidar - is it perhaps nested inside another non-public type? Can you define "aint working" - any specific error message?
Marc Gravell
The value coming back from the Settings.Default.Foo is always null - and I can't figure out why - I do have a field marked as [NonSerialized()] but I don't think that should have any effect.
Vidar
@Vidar - `XmlSerializer` doesn't even look at `[NonSerialized]`. It is impossible to answer without more details (ideally sample code).
Marc Gravell
Yep - it should be [XmlIgnore()] I think I need to read up a bit more.
Vidar
A: 

Which type is SqlDatabase?

Edit: In the user settings.

Because I tried this with type System.Object but it doesn't work. :( I also have a custom class and want to save it in the user settings.