I have a app.config which contains repeating tags as shown below:
<filelist>
<file>
<filetype>xxx</filetype>
<hex>yyyy</hex>
</file>
<file>
<filetype>aaa</filetype>
<hex>bbb</hex>
</file>
</filelist>
Can anyone suggest a way to read these values out and say load them into a
arraylist?
You'd have to write your own custom config section handler - and it's
really not that frightening as it may sound!
Basically, you need a class which implements the
"IConfigurationSectionHandler " interface. This is a really simple
interface, which contains only a single method - "Create". That method
takes a XmlNode from your config files, and passes it to your class -
you then need to parse that and return some arbitrary object (e.g. an
ArrayList).
So your section handler would look something like:
public class MyFileListHandler : IConfigurationSectionHandler
{
public object Create(object parent, object ConfigContext, XmlNode
section)
{
ArrayList alsResult = new ArrayList();
// you get the node containing the <filelist>.....</filelist>
// so iterate over all <file> tags contained in it
XmlNodeList oListOfFiles = section.SelectNodes("//file");
// for each <file> node, grab its <filetype> and <hex>
// child nodes and store into your custom object
foreach(XmlNode oSingleFileNode in oListOfFiles)
{
// grab the child nodes
XmlNode oFileType =
oSingleFileNode.SelectSingleNode("//filetype");
string sFileType = oFileType.Value;
XmlNode oHex = oSingleFileNode.SelectSingleNode("//hex");
string sHex = oHex.Value;
alsResult.Add(new FileTypeHex(sFileType, sHex));
}
return alsResult;
}
}
And lastly, you need to define this custom section in your app.config
file, so that the .NET configuration system will know about it:
<configuration>
<configSections>
<section name="filelist"
type="YourAssembly.MyFileListHandler" />
</configSections>
<filelist>
<file>
<filetype>xxx</filetype>
<hex>yyyy</hex>
</file>
<file>
<filetype>aaa</filetype>
<hex>bbb</hex>
</file>
</filelist>
</configuration>
That should work and it's really quite easy, once you've done it and
you've gotten the hang of it !
Marc
================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch