日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
      軟件
      軟件
      文章
      搜索

      首頁編程開發(fā)其它知識 → 通過通用組件來完成節(jié)點(diǎn)配置及讀取操作

      通過通用組件來完成節(jié)點(diǎn)配置及讀取操作

      相關(guān)軟件相關(guān)文章發(fā)表評論 來源:本站整理時間:2010/12/6 19:30:41字體大。A-A+

      作者:佚名點(diǎn)擊:68次評論:0次標(biāo)簽: linqtoxml LINQtoSQL app.config web.config

      ASPhere(web.config文件編輯器)V2.2.5 免費(fèi)綠色英文版
      • 類型:編程工具大。1.0M語言:英文 評分:5.0
      • 標(biāo)簽:
      立即下載
       在app.config和web.config中我們通常要用自定義節(jié)點(diǎn)來完成想要擴(kuò)展的配置內(nèi)容,以前我們需要繼承ConfigurationSection,ConfigurationElementCollection,ConfigurationElement這些類來完成配置信息的讀取代碼比較繁瑣復(fù)雜。后來有了linq to xml 通過它我們很容易查詢出配置信息,但是這樣卻少了類的定義,只能通過XmlNode來讀取或者填充我們定義的類。

      一、原來自定配置文件的編寫方式:

      01 1、定義類型比較繁瑣

      02

      03 internal class AOPConfigurationSection : ConfigurationSection

      04 {

      05 [ConfigurationProperty("", IsDefaultCollection = true)]

      06 public AopElementCollection Aops //需要定義這里略

      07 {

      08 get

      09 {

      10 return (AopElementCollection)base[""];

      11 }

      12 }

      13 }

      1 2、LINQ TO XML查詢

      2

      3 XElement xelement = XElement.Parse(xml);

      4 var name = from e in xelement.Elements("b")

      5 let s = e.Element("e")

      6 select s.Attribute("name").Value;

      二、通用配置組件介紹

      引用:DefinitionConfig.dll

      對象:

      DisplayConfigName特性(對應(yīng)節(jié)點(diǎn)名稱)

      ReadSection類(初始化節(jié)點(diǎn))

      ConfigSectionsHelper類(配置解析類)

      Sections類(配置集合類)

      特點(diǎn):根據(jù)自定義類型讀取配置文件

      注意:定義屬性為string或class類型,集合為泛型Sections類

      方法:GetConfigSection<ConnConfig>();//讀取唯一節(jié)點(diǎn)類型

      GetConfigSectionChild<ConnConfig>();//讀取包含子節(jié)點(diǎn)類型

      1 1.自定義類型

      2

      3 public class ConnConfig

      4 {

      5 [DisplayConfigName("name")]//配置文件中名稱為name

      6 public string Name { get; set; }

      7 public string str{ get; set; }//如果沒有聲明特性,那么配置文件名稱為屬性名稱str

      8 }

      01 2.單節(jié)點(diǎn)(不含子節(jié)點(diǎn))

      02

      03 //這里section 中type屬性為DefinitionConfig.ConfigSectionsHelper,DefinitionConfig

      04

      05 <configSections>

      06 <section name="connstr" type="DefinitionConfig.ConfigSectionsHelper,DefinitionConfig"/>

      07

      08 </configSections>

      09

      10 <connstr name="db" str="connstring"></connstr>

      11

      12 或

      13

      14 <connstr str="connstring">

      15 <name>db</name>

      16

      17 <str>connstring</str>

      18 </connstr>

      19

      20 上面我們只配置了一個connstr節(jié)點(diǎn)。沒有任何子節(jié)點(diǎn)。注:此節(jié)點(diǎn)只能有一個,所以不能多個connstr。

      21

      22 我們把這個配置讀取為ConnConfig類型。

      23

      24 ReadSection rs = new ReadSection("connstr");//實(shí)例化ReadSection,參數(shù)為節(jié)點(diǎn)名稱

      25

      26 ConnConfig conn= rs.GetConfigSection<ConnConfig>();//通過GetConfigSection讀取配置

      27

      28 Console.WriteLine(conn.Name);//驗證是否讀取到

      01 3、多節(jié)點(diǎn)(含子節(jié)點(diǎn))

      02

      03 <configSections>

      04 <section name="connstrs" type="DefinitionConfig.ConfigSectionsHelper,DefinitionConfig"/>

      05

      06 </configSections>

      07

      08 <connstrs>

      09

      10 <conn name=”sql” str=”connstring”></conn>

      11

      12 <conn name=”mysql”>

      13

      14 <str>connstring</str>

      15

      16 </conn>

      17

      18 <conn>

      19

      20 <name>sqlite</name>

      21

      22 <str>connstring</str>

      23

      24 </conn>

      25

      26 </connstrs>

      27

      28 ReadSection rs = new ReadSection("connstrs");//讀取connstrs節(jié)點(diǎn)

      29 var con = rs.GetConfigSectionChild<ConnConfig>();//GetConfigSectionChild讀取子節(jié)點(diǎn)配置,注:只要有子節(jié)點(diǎn)配置都需要用這個來讀取

      30 foreach (var item in con)

      31 {

      32 Console.WriteLine(item.Name);

      33 Console.WriteLine(item.str);

      34 }

      01 4、屬性為自定義類型(含多個子節(jié)點(diǎn))

      02

      03 public class ConnConfig

      04 {

      05 [DisplayConfigName("name")]

      06 public string Name { get; set; }

      07 public ConnString str { get; set; }//定義為類型

      08 }

      09

      10 public class ConnString {

      11 public string name { get; set; }

      12 public string type { get; set; }

      13 }

      14

      15 <connstrs>

      16 <con>

      17 <name>oracle</name>

      18 <str name="oracledb">

      19 <type>oracle10</type>

      20 </str>

      21 </con>

      22

      23 </connstrs>

      24

      25 ReadSection rs = new ReadSection("connstrs");

      26 var con = rs.GetConfigSectionChild<ConnConfig>();

      27 Console.WriteLine(con[0].str.name);//oracledb

      01 5、屬性為自定義集合類型(子節(jié)點(diǎn)集合)

      02

      03 public class ConnConfig

      04 {

      05 [DisplayConfigName("name")]

      06 public string Name { get; set; }

      07 public ConnString str { get; set; }

      08 public Sections<AA> aa { get; set; }//定義集合

      09 }

      10

      11 public class ConnString {

      12 public string name { get; set; }

      13 public string type { get; set; }

      14 }

      15

      16 public class AA{

      17 public string name{get;set;}

      18 }

      19

      20 <connstrs>

      21 <con>

      22 <name>oracle</name>

      23 <str name="oracledb">

      24 <type>oracle10</type>

      25 </str>

      26 <aa name="1"></aa>

      27 <aa>

      28 <name>2</name>

      29 </aa>

      30 </con>

      31

      32 </connstrs>

      33

      34 ReadSection rs = new ReadSection("connstrs");

      35 var con = rs.GetConfigSectionChild<ConnConfig>();

      36 foreach (var item in con[0].aa)

      37 {

      38 Console.WriteLine(item.name);

      39 }

      01 6、屬性為自定義多個集合類型(多子節(jié)點(diǎn)集合)

      02

      03 public class ConnConfig

      04 {

      05 [DisplayConfigName("name")]

      06 public string Name { get; set; }

      07 public ConnString str { get; set; }

      08 public Sections<AA> aa { get; set; }

      09 }

      10

      11 public class ConnString {

      12 public string name { get; set; }

      13 public string type { get; set; }

      14 }

      15

      16 public class AA

      17 {

      18 public string name { get; set; }

      19 public Sections<BB> bb { get; set; }

      20 }

      21

      22 public class BB

      23 {

      24 public string type { get; set; }

      25 }

      26

      27 <connstrs>

      28 <con>

      29 <name>oracle</name>

      30 <str name="oracledb">

      31 <type>oracle10</type>

      32 </str>

      33 <aa name="1">

      34 <bb type="type1"></bb>

      35 </aa>

      36 <aa>

      37 <name>2</name>

      38 <bb type="type2"></bb>

      39 </aa>

      40 </con>

      41

      42 </connstrs>

      43

      44 ReadSection rs = new ReadSection("connstrs");

      45 var con = rs.GetConfigSectionChild<ConnConfig>();

      46 foreach (var item in con[0].aa)

      47 {

      48 Console.WriteLine(item.name);

      49 }

      1 7、配置外部config

      2

      3 <section name="mySection" type="DefinitionConfig.ConfigSectionsHelper,DefinitionConfig" requirePermission="false" restartOnExternalChanges="false"/>

      4

      5 <mySection configSource="mySection.config"/>

      6

      7 ReadSection rs = new ReadSection("mySection");//讀取節(jié)點(diǎn)

      8 var list = rs.GetConfigSectionChild<ConfigItem>();

      組件下載:DefinitionConfig

      組件Demo:ReadConfigDemo

        相關(guān)評論

        閱讀本文后您有什么感想? 已有人給出評價!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過難過
        • 5 囧
        • 3 圍觀圍觀
        • 2 無聊無聊

        熱門評論

        最新評論

        發(fā)表評論 查看所有評論(0)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)