7
\$\begingroup\$

I'm trying to figure out how to do this more dynamically. Right now I save each and every form field individually/manually. I would love to maybe have some kind of master form list that I could loop through. I'm fairly new to C#, so I don't know many tricks yet. Please let me know what you think.

Here is my simplified version of my code:

Note: None of the variable names are numbered in my real code. I changed them for simplicity in my example. So I can't loop through form names by iterating.

//BINDED FORM COLLECTION 
public class FormLink
{
    private string _fObj1; 
    private string _fObj2; 
    private string _fObj3; 
    private string _fObj4; 
    private bool _fObj5; 
    private bool _fObj6; 
    private bool _fObj7; 

    public string fObj1
    {
        get
        {
            return this._fObj1;
        }
        set
        {
            this._fObj1 = value;
        }
    }

    public string fObj2 { /*...*/ }
    public string fObj3 { /*...*/ }
    public string fObj4 { /*...*/ }
    public bool fObj5 { /*...*/ }
    public bool fObj6 { /*...*/ }
    public bool fObj7 { /*...*/ }
}

//SETTINGS HANDLE
public class Settings
{
    private string SettingsFile = "settings.xml"; 
    FormLink form; 

    public Settings(FormLink form)
    {
        this.form = form; 
    }

    public void iStart()
    {
        if (!File.Exists(this.SettingsFile))
        {
            this.createDefaultsFile();
        }

        this.iLoad(); 
    }

    public void iEnd()
    {
        this.alterNodeValue(this.SettingsFile, "Settings", "fObj1", this.form.fObj1.ToString());
        this.alterNodeValue(this.SettingsFile, "Settings", "fObj2", this.form.fObj2.ToString());
        this.alterNodeValue(this.SettingsFile, "Settings", "fObj3", this.form.fObj3.ToString());
        this.alterNodeValue(this.SettingsFile, "Settings", "fObj4", this.form.fObj4.ToString());
        this.alterNodeValue(this.SettingsFile, "Settings", "fObj5", this.form.fObj5.ToString());
        this.alterNodeValue(this.SettingsFile, "Settings", "fObj6", this.form.fObj6.ToString());
        this.alterNodeValue(this.SettingsFile, "Settings", "fObj7", this.form.fObj7.ToString());
    }

    private void createDefaultsFile()
    {
        XDocument xml = new XDocument(
            new XElement("Settings",
                new XElement("fObj1", "string"),
                new XElement("fObj2", "string"),
                new XElement("fObj3", "string"),
                new XElement("fObj4", "string"),
                new XElement("fObj5", false),
                new XElement("fObj6", false),
                new XElement("fObj7", false),
            ));
        xml.Save(this.SettingsFile, SaveOptions.None);
    }

    private void iLoad()
    {
        var settings = this.getNodes(XDocument.Load(this.SettingsFile, "Settings");

        this.form.fObj1 = Help.getDictVal(settings, "fObj1");
        this.form.fObj1 = Help.getDictVal(settings, "fObj2");
        this.form.fObj1 = Help.getDictVal(settings, "fObj3");
        this.form.fObj1 = Help.getDictVal(settings, "fObj4");
        this.form.fObj1 = Help.getDictVal(settings, Help.stringToBool("fObj5"));
        this.form.fObj1 = Help.getDictVal(settings, Help.stringToBool("fObj6"));
        this.form.fObj1 = Help.getDictVal(settings, Help.stringToBool("fObj7"));
    }

    private void alterNodeValue(string xmlFile, string parent, string node, string newVal) 
    { 
        /* Alters a single XML Node and saves XML File */ 
    }

    private Dictionary<string, string> getNodes(XDocument xml, string parent) 
    { 
        /* Retrieves all Child Nodes of specified Parent and returns them in a Dictionary */ 
    }
}

//Basic Utilies class
public static class Help
{
    public static bool stringToBool(string BoolMe)
    {
        /* Safely converts String to Bool */
    }

    public static string getDictVal(Dictionary<string, string> dict, string key)
    {
        /* Safely gets value from Dictionay based on Key*/
    }
}
\$\endgroup\$
7
\$\begingroup\$

you can just use an XML serializer?

var s = new XmlSerializer(typeof(FormLink));
TextWriter w = new StreamWriter("settings.xml");
s.Serialize(w, form);
w.Close();
\$\endgroup\$
  • \$\begingroup\$ Hmmm... that's a very very interesting idea... And I like it. Especially the part where I can customize how each property is saved(element, attribute, etc..). In order to make that work though, I would need to break FormLink into sections that would be serialized separately. Due to the nature of my application, I have to be able to save sections of settings in separate xml files. I can't have them all in 1 file. Do you have any idea how I could do that? \$\endgroup\$ – PiZzL3 Apr 11 '11 at 15:32
  • \$\begingroup\$ +1 for using XmlSerializer. you can add ignore attribute to fields and add logic to setters and getters of properties using those fields \$\endgroup\$ – vittore Apr 12 '11 at 18:19
4
\$\begingroup\$

It looks like what you're trying to do here is possible by using .NET's built-in support for application settings. Check out this link for details on how to use it. Essentially, you can define your settings within a App.config XML file in a standard format, and then retrieve them with the built-in ConfigurationManager class.

\$\endgroup\$
  • \$\begingroup\$ Unfortunately that's not gonna work for what I'm doing. App.config and Settings.config are nice but have too many limitations for what I'm doing. The above code is just a simplified version of what I'm using just to make my question clear. \$\endgroup\$ – PiZzL3 Apr 11 '11 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.