hugoduraes
asked on
Dynamic Variable Assignment in C#
I am reading settings from a XML file:
<settings>
<setting1>value1</setting1 >
<setting2>value2</setting2 >
</settings>
What I want is to read the XML file and crete dinamically variables with the XML element name.
In the end I should have a variable called "setting1" with value "value1", another variable called "setting2" with value "value2". If I execute Console.WriteLine(setting2 ) it should output "value1".
Is there a way to do this?
Thanks
<settings>
<setting1>value1</setting1
<setting2>value2</setting2
</settings>
What I want is to read the XML file and crete dinamically variables with the XML element name.
In the end I should have a variable called "setting1" with value "value1", another variable called "setting2" with value "value2". If I execute Console.WriteLine(setting2
Is there a way to do this?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
AlexFM is spot on with hash table.
It sounds like what you want already exists. In 2.0 when you add elements to the *.settings file it dynamically allows you to access your settings through properties. so,
If I add two values to my settings file. BTW, they are type safe.
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="J UNK__Conso leApplicat ion1.Prope rties" GeneratedClassName="Settin gs">
<Profiles />
<Settings>
<Setting Name="MyName" Type="System.String" Scope="User">
<Value Profile="(Default)">gary</ Value>
</Setting>
<Setting Name="MyBirthday" Type="System.DateTime" Scope="User">
<Value Profile="(Default)">1982-0 2-01</Valu e>
</Setting>
</Settings>
</SettingsFile>
I can immediatly access them in code like this,
string name = Properties.Settings.Defaul t.MyName;
DateTime birthDay = Properties.Settings.Defaul t.MyBirthd ay;
The underlying code gened looks like,,
[global::System.Runtime.Co mpilerServ ices.Compi lerGenerat edAttribut e()]
[global::System.CodeDom.Co mpiler.Gen eratedCode Attribute( "Microsoft .VisualStu dio.Editor s.Settings Designer.S ettingsSin gleFileGen erator", "8.0.0.0")]
internal sealed partial class Settings : global::System.Configurati on.Applica tionSettin gsBase {
private static Settings defaultInstance = ((Settings)(global::System .Configura tion.Appli cationSett ingsBase.S ynchronize d(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configurat ion.UserSc opedSettin gAttribute ()]
[global::System.Diagnostic s.Debugger NonUserCod eAttribute ()]
[global::System.Configurat ion.Defaul tSettingVa lueAttribu te("gary") ]
public string MyName {
get {
return ((string)(this["MyName"])) ;
}
set {
this["MyName"] = value;
}
}
[global::System.Configurat ion.UserSc opedSettin gAttribute ()]
[global::System.Diagnostic s.Debugger NonUserCod eAttribute ()]
[global::System.Configurat ion.Defaul tSettingVa lueAttribu te("1982-0 2-01")]
public global::System.DateTime MyBirthday {
get {
return ((global::System.DateTime) (this["MyB irthday"]) );
}
set {
this["MyBirthday"] = value;
}
}
}
gp
If I add two values to my settings file. BTW, they are type safe.
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)"
<Profiles />
<Settings>
<Setting Name="MyName" Type="System.String" Scope="User">
<Value Profile="(Default)">gary</
</Setting>
<Setting Name="MyBirthday" Type="System.DateTime" Scope="User">
<Value Profile="(Default)">1982-0
</Setting>
</Settings>
</SettingsFile>
I can immediatly access them in code like this,
string name = Properties.Settings.Defaul
DateTime birthDay = Properties.Settings.Defaul
The underlying code gened looks like,,
[global::System.Runtime.Co
[global::System.CodeDom.Co
internal sealed partial class Settings : global::System.Configurati
private static Settings defaultInstance = ((Settings)(global::System
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configurat
[global::System.Diagnostic
[global::System.Configurat
public string MyName {
get {
return ((string)(this["MyName"]))
}
set {
this["MyName"] = value;
}
}
[global::System.Configurat
[global::System.Diagnostic
[global::System.Configurat
public global::System.DateTime MyBirthday {
get {
return ((global::System.DateTime)
}
set {
this["MyBirthday"] = value;
}
}
}
gp
ASKER
devsolns: And how can I create or add elements to the *.settings file?
If a settings file does not already exist in your project then add new item->settings file. When you double click it will open up a grid for easy entry of items. You can also choose to open it in normal xml view if you'd like.
You didnt specify a requirment to generate this at runtime. To generate a class at runtime is certainly possible using either Codedom or EMIT but may add a lot of complexity where there shouldnt be.
ASKER
Ok... I'm beggining to like this solution! ;)
Just one more question... can I change settings value? I have to allow users to change these settings values...
Just one more question... can I change settings value? I have to allow users to change these settings values...
If you change the "value" then you will need to recompile! Yeah that is probably the biggest limitation.
ASKER
I'll have to stick with hashtables then...
Thanks ;)
Thanks ;)
dont agree, next step was ConfigurationManager which is better than hash table but o'well.
ASKER
With ConfigurationManager can I change configuration values during runtime? Or do I need to recompile?
You can change them at runtime.