生活随笔
收集整理的這篇文章主要介紹了
配置文件configSections节点使用实例 。
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
configSections為自定義節(jié)點(diǎn),增加應(yīng)用程序可移植性,用于配置文件上傳路徑,再深入應(yīng)用可定義工廠方法需要加載創(chuàng)建的類。
1.配置configSections節(jié)點(diǎn)
[html] view plaincopy print?
<configSections>??????<section?name?="MyName"?type="LearningConfiguration.NameSectionHandler"/>??</configSections>??<MyName>??????<Add?key="lu"?name="lulu"></Add>??????<Add?key="lu2"?name="66"></Add>??</MyName>?? <configSections><section name ="MyName" type="LearningConfiguration.NameSectionHandler"/>
</configSections>
<MyName><Add key="lu" name="lulu"></Add><Add key="lu2" name="66"></Add>
</MyName>
2.定義NameSectionHandler類實(shí)現(xiàn)IConfigurationSectionHandler接口
[csharp] view plaincopy print?
namespace?LearningConfiguration??{??????public?class?NameSectionHandler?:?IConfigurationSectionHandler??????{??????????#region?隱式實(shí)現(xiàn)接口??????????public?object?Create(object?parent,?object?configContext,?System.Xml.XmlNode?section)??????????{??????????????Dictionary<string,?string>?names?=?new?Dictionary<string,?string>();??????????????string?key?=?string.Empty;??????????????string?name?=?string.Empty;??????????????????????????????foreach?(XmlNode?childNode?in?section.ChildNodes)??????????????{??????????????????if?(childNode.Attributes["key"]?!=?null)??????????????????{??????????????????????key?=?childNode.Attributes["key"].Value;????????????????????????if?(childNode.Attributes["name"]?!=?null)??????????????????????{??????????????????????????name?=?childNode.Attributes["name"].Value;??????????????????????}??????????????????????else??????????????????????{??????????????????????????name?=?string.Empty;??????????????????????}????????????????????????names.Add(key,?name);??????????????????}??????????????}????????????????return?names;??????????}??????????#endregion??????}??}?? namespace LearningConfiguration
{public class NameSectionHandler : IConfigurationSectionHandler{#region 隱式實(shí)現(xiàn)接口public object Create(object parent, object configContext, System.Xml.XmlNode section){Dictionary<string, string> names = new Dictionary<string, string>();string key = string.Empty;string name = string.Empty;//獲取配置文件中自定義節(jié)點(diǎn)值foreach (XmlNode childNode in section.ChildNodes){if (childNode.Attributes["key"] != null){key = childNode.Attributes["key"].Value;if (childNode.Attributes["name"] != null){name = childNode.Attributes["name"].Value;}else{name = string.Empty;}names.Add(key, name);}}return names;}#endregion}
}
?
3.調(diào)用
[csharp] view plaincopy print?
protected?void?Page_Load(object?sender,?EventArgs?e)??{??????Dictionary<string,?string>?names?=?ConfigurationManager.GetSection("MyName")?as?Dictionary<string,?string>;??????if?(names?!=?null)??????{????????????????????????foreach?(string?key?in?names.Keys)??????????{??????????????Response.Write(names[key]);??????????}??????}??}?? protected void Page_Load(object sender, EventArgs e)
{Dictionary<string, string> names = ConfigurationManager.GetSection("MyName") as Dictionary<string, string>;if (names != null){//輸出:lulu66foreach (string key in names.Keys){Response.Write(names[key]);}}
}
總結(jié)
以上是生活随笔為你收集整理的配置文件configSections节点使用实例 。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。