patterncsharpMinor
XML settings implementation
Viewed 0 times
implementationsettingsxml
Problem
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());
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());
Solution
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.Context
StackExchange Code Review Q#1772, answer score: 4
Revisions (0)
No revisions yet.