JohnnyKnoxville
asked on
app.config (SIMPLE)
I am planning on using the app.config file to read in some user defined data. I have a number of form names that will continually change over time.
Can I have the same key listed twice?
<add key=FormName value=RothIRA>
<addkey=FormName value=TOA>
etc...
I was hoping to loop through (in code) and check for all of the FormNames. Anything found in the appConfig.....I'll be applying logic to in my app.
Is it possible to list multiple entries with the same key name? Is there a better way to go about this?
thx.
Knoxxx
Can I have the same key listed twice?
<add key=FormName value=RothIRA>
<addkey=FormName value=TOA>
etc...
I was hoping to loop through (in code) and check for all of the FormNames. Anything found in the appConfig.....I'll be applying logic to in my app.
Is it possible to list multiple entries with the same key name? Is there a better way to go about this?
thx.
Knoxxx
Here's a sample from code that I use all the time:
[App.Config]
</xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Section1"
type="ConfigSectionHandler .ConfigSec tionHandle r, ConfigSectionHandler" />
<section name="Section2"
type="ConfigSectionHandler .ConfigSec tionHandle r, ConfigSectionHandler" />
</configSections>
<Section1 type="Namespace.Class, Namespace">
<FormName>MyForm1</FormNam e>
<FormName>MyForm2</FormNam e>
<FormName>MyForm3</FormNam e>
<FormName>MyForm4</FormNam e>
<FormName>MyForm5</FormNam e>
</Section1>
</configuration>
[App.Config]
</xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Section1"
type="ConfigSectionHandler
<section name="Section2"
type="ConfigSectionHandler
</configSections>
<Section1 type="Namespace.Class, Namespace">
<FormName>MyForm1</FormNam
<FormName>MyForm2</FormNam
<FormName>MyForm3</FormNam
<FormName>MyForm4</FormNam
<FormName>MyForm5</FormNam
</Section1>
</configuration>
[Add this class to your application]
using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;
using System.Configuration;
namespace ConfigSectionHandler
{
/// <summary>
/// This class contains only one method, Create().
/// This is designed to handle custom sections in an Applications Configuration file.
/// By Implementing <see cref="IConfigurationSectio nHandler"/ >, we can implement the
/// Create method, which will provide the XmlNode from the configuration file. This is
/// Deserialized into an object and passed back to the Caller.
/// </summary>
/// <example>
/// Here is a configuration file entry in the <c>configSections</c> sectikon of the <c>App.Config</c>
/// file.
///<code> ///
///<section name="ServerConfig" type="ConfigSectionHandler .ConfigSec tionHandle r, ConfigSectionHandler" />
///</code>
///This tells the CLR that there is a section further in, with a node name of <c>ServerConfig</c>. When this section
///is to be parsed, an object of type <c>ConfigSectionHandler.Co nfigSectio nHandler</ c> which resides in the
///assembly <c>ConfigSectionHandler</c > will be instantiated. The CLR automatically calls a method in that object
///called <c>Create</c>
///</example>
public class ConfigSectionHandler : IConfigurationSectionHandl er
{
public ConfigSectionHandler()
: base()
{
}
#region IConfigurationSectionHandl er Members
/// <summary>
/// A method which is called by the CLR when parsing the App.Config file. If custom sections
/// are found, then an entry in the configuration file will tell the runtime to call this method,
/// passing in the XmlNode required.
/// </summary>
/// <param name="parent">The configuration settings in a corresponding parent configuration section. Passed in via the CLR</param>
/// <param name="configContext">An <see cref="HttpConfigurationCon text"/> when Create is called from the ASP.NET configuration system. Otherwise,
/// this parameter is reserved and is a null reference (Nothing in Visual Basic). Passed in via the CLR</param>
/// <param name="section">The <see cref="XmlNode"/> that contains the configuration information from the configuration file.
/// Provides direct access to the XML contents of the configuration section. Passed in via the CLR.</param>
/// <returns>The Deserialized object as an object</returns>
/// <exception cref="System.Configuration .Configura tionExcept ion">The Configuration file is not well formed,
/// or the Custom section is not configured correctly, or the type of configuration handler was not specified correctly
/// or the type of object was not specified correctly.
/// or the copn</exception>
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
try
{
XPathNavigator xNav = section.CreateNavigator();
string typeOfObject = (string)xNav.Evaluate("str ing(@type) ");
Type t = Type.GetType(typeOfObject) ;
XmlSerializer ser = new XmlSerializer(t);
XmlNodeReader xNodeReader = new XmlNodeReader(section);
return ser.Deserialize(xNodeReade r);
}
catch (Exception e)
{
Console.WriteLine(e.Messag e);
if (e.InnerException.Message. Length > 0)
Console.WriteLine(e.InnerE xception.M essage);
return null;
}
}
#endregion
}
}
using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;
using System.Configuration;
namespace ConfigSectionHandler
{
/// <summary>
/// This class contains only one method, Create().
/// This is designed to handle custom sections in an Applications Configuration file.
/// By Implementing <see cref="IConfigurationSectio
/// Create method, which will provide the XmlNode from the configuration file. This is
/// Deserialized into an object and passed back to the Caller.
/// </summary>
/// <example>
/// Here is a configuration file entry in the <c>configSections</c> sectikon of the <c>App.Config</c>
/// file.
///<code> ///
///<section name="ServerConfig" type="ConfigSectionHandler
///</code>
///This tells the CLR that there is a section further in, with a node name of <c>ServerConfig</c>. When this section
///is to be parsed, an object of type <c>ConfigSectionHandler.Co
///assembly <c>ConfigSectionHandler</c
///called <c>Create</c>
///</example>
public class ConfigSectionHandler : IConfigurationSectionHandl
{
public ConfigSectionHandler()
: base()
{
}
#region IConfigurationSectionHandl
/// <summary>
/// A method which is called by the CLR when parsing the App.Config file. If custom sections
/// are found, then an entry in the configuration file will tell the runtime to call this method,
/// passing in the XmlNode required.
/// </summary>
/// <param name="parent">The configuration settings in a corresponding parent configuration section. Passed in via the CLR</param>
/// <param name="configContext">An <see cref="HttpConfigurationCon
/// this parameter is reserved and is a null reference (Nothing in Visual Basic). Passed in via the CLR</param>
/// <param name="section">The <see cref="XmlNode"/> that contains the configuration information from the configuration file.
/// Provides direct access to the XML contents of the configuration section. Passed in via the CLR.</param>
/// <returns>The Deserialized object as an object</returns>
/// <exception cref="System.Configuration
/// or the Custom section is not configured correctly, or the type of configuration handler was not specified correctly
/// or the type of object was not specified correctly.
/// or the copn</exception>
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
try
{
XPathNavigator xNav = section.CreateNavigator();
string typeOfObject = (string)xNav.Evaluate("str
Type t = Type.GetType(typeOfObject)
XmlSerializer ser = new XmlSerializer(t);
XmlNodeReader xNodeReader = new XmlNodeReader(section);
return ser.Deserialize(xNodeReade
}
catch (Exception e)
{
Console.WriteLine(e.Messag
if (e.InnerException.Message.
Console.WriteLine(e.InnerE
return null;
}
}
#endregion
}
}
[Create a class that you serialize your app.config data to]
using System;
using System.Collections.Generic ;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace Application1
{
[XmlRoot("Section1")]
public class FirstSection
{
[XmlElement("FormName")]
public string[] _formName; }
}
using System;
using System.Collections.Generic
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace Application1
{
[XmlRoot("Section1")]
public class FirstSection
{
[XmlElement("FormName")]
public string[] _formName; }
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No, each key must be unique.
On the other hand, if you use XML, and serialize the data into a class, you can easily create an array of values.