You can use configuration sections where you can define your own configuration. Just add
<configSections> <sectionGroup name="MyConfiguration"> <section name="MyQuery" type="namespace.QueryConfigurationSection" allowLocation="true" allowDefinition="Everywhere"/> </sectionGroup> </configSections>
after <configuration> , and you can add your own section right after appsetting
</appSettings> <MyConfiguration> <MyQuery> <Query1> </Query1> <Query2> </Query2>
For reading you need to create some classes
/// <summary> /// Creates a custom configuration section inside web.config /// </summary> public class QueryConfigurationSection : ConfigurationSection { //query 2 [ConfigurationProperty("Query1")] public QueryElement1 Query1 { get { return this["Query1"] as QueryElement1; } } //query 2 [ConfigurationProperty("Query2")] public QueryElement2 Query2 { get { return this["Query2"] as QueryElement2; } } } public class QueryElement1 : ConfigurationElement { public string Value { get; private set; } protected override void DeserializeElement(XmlReader reader, bool s) { Value = reader.ReadElementContentAs(typeof(string), null) as string; } } public class QueryElement2 : ConfigurationElement { public string Value { get; private set; } protected override void DeserializeElement(XmlReader reader, bool s) { Value = reader.ReadElementContentAs(typeof(string), null) as string; } }
An overridden deserialized element deserializes Xml (internally) QueryElement1 and 2.
To read the values ββfrom the main application, you just need to call the following:
//calling my query config QueryConfigurationSection wconfig = (QueryConfigurationSection)ConfigurationManager.GetSection("MyConfiguration/MyQuery"); string _query1 = wconfig.Query1.Value; string _query2 = wconfig.Query2.Value;
Vinod srivastav
source share