Define a Regular Expresson within a Custom Configuration Section

G

grant.trevor

I have a need to define a regular expression within a custom
configuration section. This can reside within a web.config or
other .config file.

Looking at the config below you can see the general Idea is to define
a uri then run a Regex against the response of a request to that uri.

The attached CustomConfigurationHandler is what I'm using to access
this config. The issue I have at the moment is how to access the CDATA
component of the pattern element. NB The code I've supplied actually
expects the pattern element to be a property of the the test element,
I've tried switching this around by making the pattern a
ConfigurationElement itself however this has result in the following
error:

"The configuration section cannot contain a CDATA or text element"

At the end of the day I'm open to any and all suggestions.

site.config

<configuration>
<configSections>
<section name="TestConfiguration"
type="SampleSite.Code.CustomConfiguration.SiteTestConfigurationHandler,SampleSite"/ </configSections>
<TestConfiguration>
<test uri="http://www.test.com/">
<pattern><![CDATA[<title>(?<title>\w*)</title>]]></pattern>
</test>
<test uri="http://www.asp.net/">
<pattern><![CDATA[<title>(?<title>\w*)</title>]]></pattern>
</test>
</TestConfiguration>
</configuration>


SiteTestConfigurationHandler

namespace AutomatedWebRequestTool.Code.CustomConfiguration
{
public class SiteTestConfigurationHandler : ConfigurationSection
{
[ConfigurationProperty("", IsRequired=true, IsKey=false,
IsDefaultCollection=true)]
public SiteTestConfigurationElementCollection SiteTests
{
get { return base[""] as
SiteTestConfigurationElementCollection; }
set { base[""] = value; }
}
}

public class SiteTestConfigurationElement : ConfigurationElement
{
[ConfigurationProperty("uri", IsRequired = true,IsKey=true)]
public String Uri
{
get { return (String)this["uri"]; }
set { this["uri"] = value; }
}

[ConfigurationProperty("pattern", IsRequired = true, IsKey =
false)]
public string Pattern
{
get { return (string)this["pattern"]; }
set { this["pattern"] = value; }
}
}

public class SiteTestConfigurationElementCollection :
ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new SiteTestConfigurationElement();
}

protected override object GetElementKey(ConfigurationElement
element)
{
return ((SiteTestConfigurationElement)element).Uri;
}

protected override string ElementName
{
get
{
return "siteTest";
}
}

protected override bool IsElementName(string elementName)
{
return !String.IsNullOrEmpty(elementName) && elementName
== "siteTest";
}

public override ConfigurationElementCollectionType
CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}


public SiteTestConfigurationElement this[int index]
{
get
{
return base.BaseGet(index) as
SiteTestConfigurationElement;
}
}
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top