1. The program calls ConfigurationManager. GetSection (sectionName)
2. Handler implements the Create (object parent, object configContext, XmlNode section) method of IConfigurationSectionHandler.
3. parse attribute & subNode of section through section XmlNode
Running effect:
The Code is as follows:
1. Call Method
Protected void Page_Load (object sender, EventArgs e) {// get ConfigManager-type instance ConfigManager config = (ConfigManager) ConfigurationManager. getSection ("traceFact"); ltrName. text = config. forumConfig. name; ltrOfflineTime. text = config. forumConfig. offlineTime. toString (); ltrPageSize. text = config. forumConfig. pageSize. toString (); ltrReplyCount. text = config. forumConfig. replyCount. toString (); ltrRootUrl. text = config. forumConfig. rootUrl. toString ();}
2. Web. Config
<Section name = "traceFact" type = "CustomConfig. generalConfigurationHandler, CustomConfig "/> <traceFact type =" CustomConfig. configManager, CustomConfig "> <forum name =" TraceFact. net Community "> <root url =" http://forum.tracefact.net "/> <replyCount> 20 </replyCount> <pageSize> 30 </pageSize> <offlineTime> 20 </offlineTime> </forum> <blog name = "JimmyZhang's Space"> <root url = "http://blog.tracefact.net"/> <urlMappings> <r EwriteRule> <request> ~ /(\ D {4})/Default \. aspx </request> <sendTo> ~ /BlogDetail. aspx? Year = $1 </sendTo> </rewriteRule> </urlMappings> </blog> <! -- Include the preceding configuration --> <mailServer address = "mail.tracefact.net" userName = "jimmyzhang" password = "123456"/> <greetingStrategy type = "ClassLib. chineseGreeting, ClassLib "/> </traceFact>
3. Handler code
Public class GeneralConfigurationHandler: IConfigurationSectionHandler {// The section node here is Web. greetingStrategy node public object Create (object parent, object configContext, XmlNode section) in Config {// obtain the value of the node type attribute Type t = Type. getType (section. attributes ["type"]. value); // directly pass the section to object [] parameters = {section}; // type instance to be created object obj = null; try {obj = Activator. createInstance (t, parameters); // use a constructor with parameters} catch (Exception ex) {return null;} // obj is the object defined in the type attribute of the node, here is ClassLib. chineseGreetingreturn obj ;}} public class ConfigManager {private XmlNode section; private ForumConfiguration forumConfig; // private BlogConfiguration blogConfig; // private MailServerConfiguration mailServerConfig; // private IGreetingStrategy greetingStrategy; // similar to the following, omitted... public ForumConfiguration ForumConfig {get {return forumConfig; }}// public BlogConfiguration BlogConfig {// get {return blogConfig;} //} public ConfigManager (XmlNode section) {this. section = section; forumConfig = new ForumConfiguration (section. selectSingleNode ("forum"); // blogConfig = new BlogConfiguration (section. selectSingleNode ("blog"); // mailServerConfig = new MailServerConfiguration (section. selectSingleNode ("mailServer"); // similar to the following, omitted ...}} // specific sub-node configuration forum node public class ForumConfiguration {private XmlNode forumNode; // pass the forum node in public ForumConfiguration (XmlNode section) {this. forumNode = section;} public string Name {get {return forumNode. attributes ["name"]. value ;}} public string RootUrl {get {return forumNode. selectSingleNode ("root "). attributes ["url"]. value ;}} public int PageSize {get {return int. parse (forumNode. selectSingleNode ("pageSize "). innerText) ;}} public int ReplyCount {get {return int. parse (forumNode. selectSingleNode ("replyCount "). innerText) ;}} public int OfflineTime {get {return int. parse (forumNode. selectSingleNode ("offlineTime "). innerText );}}}