对于.net中的自定义节点,如果要单独处理,就要为这个节点添加一个指定的类,如果我们在一个应用程序中,定义了很多个自定义节点的话,还按照这样做,就会多出很多个处理类来,为了避免太多的类,我们将所有自定节点都指定一个自定义节点作为入口,然后只写一个处理类,以此为入口,读取其他节点。
例如,配置文件只定义一个入口节点:
20 30 20 ~/(\d{4})/Default\.aspx ~BlogDetail.aspx?year=$1
处理类:
////// Class GeneralConfigHandler /// ///Editor:v-liuhch CreateTime:2015/7/1 10:50:40 public class GeneralConfigHandler:IConfigurationSectionHandler { ////// 创建配置节处理程序。 /// /// 父对象。 /// 配置上下文对象。 /// 节 XML 节点。 ///创建的节处理程序对象。 ////// Editor:v-liuhch CreateTime:2015/7/1 10:50:42 public object Create(object parent, object configContext, XmlNode section) { //获取节点type属性的值 Type t=Type.GetType(section.Attributes["type"].Value);//返回configManager //直接将section进行传递 object[] paramters={section}; //将要创建的类型实例 object obj = null; try { obj = Activator.CreateInstance(t, paramters);//使用有参数的构造函数 } catch (Exception) { return null; } return obj; } }
上面的类主要是为了实现创建具体配置节点管理类,在配置节点的管理类中,存有各个子节点类的引用。
一款非常包包、衣服、鞋子类网站,页面干净清洁、一目了然,mttshop打造精致、简单、易用、免费的商城。 系统要求:IIS5.1以后,必须安装.net 3.5 安装步骤: 1、下载完成后,直接解压文件mttshop.rar 2、附加数据库:解压后的可以找一个叫db的文件夹,解压后直接附加就可以,支持SQL 2000、2005、2008 3、配置web.config文件,找到key=&qu
////// GeneralConfigHandler通过反射动态创建的,它实际上只是作为一个容器 ///这个类仅仅是作为一个容器,包含对其下具体节点配置的引用,并通过traceFact根节点,获取traceFact其下的子节点,然后再创建用于映射具体的子节点的类型实例 /// ///Editor:v-liuhch CreateTime:2015/7/1 14:06:21 public class ConfigManager { private XmlNode section; private FroumConfiguration forumConfig; //对于其他节点的处理强类型访问类也定义为成员变量放在这里。。。。。。。 public FroumConfiguration froumConfig { get { return forumConfig; } } public ConfigManager(XmlNode section) { this.section = section; forumConfig = new FroumConfiguration(section.SelectSingleNode("forum")); } } ////// 具体子节点配置forum结点:处理 ////// Editor:v-liuhch CreateTime:2015/7/1 14:22:08 public class FroumConfiguration{ private XmlNode forumNode; //将forum结点传递进来 public FroumConfiguration(XmlNode section){ this.forumNode=section; } ////// Gets the name. ////// The name. ///Editor:v-liuhch CreateTime:2015/7/1 14:58:00 public string Name{ get{ return forumNode.Attributes["name"].Value; } } ////// Gets the root URL.: ////// The root URL. ///Editor:v-liuhch CreateTime:2015/7/1 14:56:08 public string RootUrl{ get{ return forumNode.SelectSingleNode("root").Attributes["url"].Value; } } ////// Gets the size of the page. ///30 ///The size of the page. ///Editor:v-liuhch CreateTime:2015/7/1 14:57:18 public int PageSize{ get{ return int.Parse(forumNode.SelectSingleNode("pageSize").InnerText); } } ////// Gets the reply count. ///20 ///The reply count. ///Editor:v-liuhch CreateTime:2015/7/1 14:57:03 public int ReplyCount{ get{ return int.Parse(forumNode.SelectSingleNode("replyCount").InnerText); } } ////// Gets the offline time. ///20 ///The offline time. ///Editor:v-liuhch CreateTime:2015/7/1 14:57:35 public int OfflineTime { get { return int.Parse(forumNode.SelectSingleNode("offlineTime").InnerText); } } }
这样,我们使用的时候,每次都用这一个类来实现对节点的访问:
ConfigManager config = ConfigurationManager.GetSection("traceFact") as ConfigManager;
//取值
ltrName.Text = config.froumConfig.Name;
ltrOfflineTime.Text = config.froumConfig.OfflineTime.ToString();
ltrPageSize.Text = config.froumConfig.PageSize.ToString();
ltrReplyCount.Text = config.froumConfig.ReplyCount.ToString();
ltrRootUrl.Text = config.froumConfig.RootUrl.ToString();以上就是.Net配置文件——统一节点配置管理的内容,更多相关内容请关注PHP中文网(www.php.cn)!









