Hi All, How are you?
I am trying to write a C# code to read a application configuration file in order to setup all parameters to create a URL Monitoring on SCOM through SCOM's SDK.
Therefore, I am trying to read a application configuration file that follows the structure bellow :
<?xml version="1.0" encoding="utf-8" ?><configuration><configSections><section name="managementPacks" type="URLMonitoringTestXml.ManagementPackSection, URLMonitoringTestXml" /></configSections><managementPacks><managementPack key="Name1"><add key="key1" value="value1" /><add key="key2" value="value2" /></managementPack><managementPack key="Name2"><add key="key1" value="value3" /><add key="key2" value="value4" /></managementPack></managementPacks></configuration>
In order to do so, I have read several tutorials on how to create a custom application config file.
And I have written the code bellow:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Xml;
using System.Collections;
namespace URLMonitoringTestXml
{
class ManagementPackSection : ConfigurationSection
{
[ConfigurationProperty("managementPack", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(ManagementPackAddElementCollection),
AddItemName = "add")]
public ManagementPackAddElementCollection properties
{
get
{
return (ManagementPackAddElementCollection)base["managementPack"];
}
}
}
// Define the "add" element
// with "key" and "value" attributes.
public class ManagementPackAddElement : ConfigurationElement
{
public ManagementPackAddElement(String key, String name)
{
this.Key = key;
this.Value = name;
}
public ManagementPackAddElement()
{
this.Key = "";
this.Value = "";
}
[ConfigurationProperty("key", IsRequired = true)]
[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\")]
public String Key
{
get
{
return (String)this["key"];
}
set
{
this["key"] = value;
}
}
[ConfigurationProperty("value", DefaultValue = "", IsRequired = true)]
[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/'\"|\\")]
public String Value
{
get
{ return (String)this["value"]; }
set
{ this["value"] = value; }
}
}
//define ManagementPackAddElement Collection
public class ManagementPackAddElementCollection : ConfigurationElementCollection
{
public ManagementPackAddElement this[int index]
{
get
{
return base.BaseGet(index) as ManagementPackAddElement;
}
set
{
if (base.BaseGet(index) != null)
{
base.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
}
protected
override System.Configuration.ConfigurationElement CreateNewElement()
{
return new ManagementPackAddElement();
}
protected override object GetElementKey(
System.Configuration.ConfigurationElement element)
{
return ((ManagementPackAddElement)element).Key;
}
public void Add(ConfigurationElement element)
{
BaseAdd(element);
}
public void Clear()
{
BaseClear();
}
}
}
So, I could not figure out how to adapt the code above to handle nested configuration elements ( I mean, managementPacks elements can have several managementPack children, and each managementPack has its parameters as children ).
Please, May someone help me?
I appreciate any help you can give me,
Rodrigo