PC Review


Reply
Thread Tools Rate Thread

Define a Regular Expresson within a Custom Configuration Section

 
 
grant.trevor@gmail.com
Guest
Posts: n/a
 
      4th Feb 2007
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;
}
}
}

}

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Configuration Section partysingh Microsoft ASP .NET 1 27th Jan 2010 04:14 PM
Custom Configuration Section Help Yan Yi Huang Microsoft Dot NET Framework 1 22nd Nov 2007 11:39 AM
Custom Configuration Section Inheritance =?Utf-8?B?UGhpbGlw?= Microsoft ASP .NET 0 27th Sep 2007 04:33 PM
web.config custom Configuration Section =?Utf-8?B?SmVycnkgQw==?= Microsoft Dot NET 3 29th Mar 2007 03:00 AM
Custom Configuration Section - .NET 2.0 =?Utf-8?B?V2lsbGlhbSBMZWFyeQ==?= Microsoft Dot NET Framework 1 22nd Nov 2005 11:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:00 PM.