关于配置文件的读取

在1.0/1.1版本,都流行的是appSettings这个节点读取;无论web.config,还是app.exe.config都可以使用这个节点配置。

如:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
  <add key="TestKey" value="(local)"/>
  <add key="WoniuguDBName" value="woniugu_test"/>
    </appSettings>
    <!--还有其他的-->
</configuration>

在代码中读取:
string 内容 = ConfigurationSettings.AppSettings[key];

这种方式,简洁;

还有一种:自定义配置节

举例说明:
<configuration>
    <configSections>
        <section name="sampleSection"
                 type="System.Configuration.SingleTagSectionHandler" />
    </configSections>

    <sampleSection setting1="Value1" setting2="value two"
                   setting3="third value" />
</configuration>

代码中读取:ConfigurationSettings.GetConfig("sampleSection");其返回的对象强制转换为 IDictionary 对象

IDictionary sampleTable = (IDictionary) ConfigurationSettings.GetConfig("sampleSection");
string value1 = (string)sampleTable["setting1"];
string value2 = (string)sampleTable["setting2"];
string value3 = (string)sampleTable["setting3"];

+++++++++++++++

在2.0版本,web.config还是流行appSettings来配置,但app.exe.config采用了Section节点配置。读取类也发生了改变。

在代码中读取:
string 内容 = ConfigurationManager.AppSettings[key];

后面,我简单测试了下,在app.exe.config中还是可以手工添加appSettings来配置。同样用上面的代码来访问,没有问题。

这里,我阅读了一篇文章.Net 自定义应用程序配置,http://www.tracefact.net/CLR-and-Framework/Custom-Application-Configuration.aspx

不错,上面很好讲解。

在此,我只贴我自己分析后写出的部分代码,用来显示如何提取,当然和《.Net 自定义应用程序配置》的内容不一样:
配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0,

Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="SetConfig.Properties.Settings" type="System.Configuration.ClientSettingsSection, System,

Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser"

requirePermission="false" />
        </sectionGroup>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System,

Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="SetConfig.Properties.Settings" type="System.Configuration.ClientSettingsSection, System,

Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <SetConfig.Properties.Settings>
            <setting name="userSetting1" serializeAs="String">
  <value>user</value>
     </setting>
        </SetConfig.Properties.Settings>
    </userSettings>
    <applicationSettings>
        <SetConfig.Properties.Settings>
            <setting name="appSetting1" serializeAs="String">
                <value>application</value>
            </setting>
        </SetConfig.Properties.Settings>
    </applicationSettings>
    <appSettings>
  <add key="TestKey" value="(local)"/>
  <add key="WoniuguDBName" value="woniugu_test"/>
    </appSettings>
</configuration>

部分读取代码:
第一种,直接读取,注意上面的配置的类型System.Configuration.ClientSettingsSection   

 
  
1 ClientSettingsSection config = ( ClientSettingsSection ) ConfigurationManager.GetSection( " userSettings/SetConfig.Properties.Settings " );
2
3   if ( config == null )
4 return ;
5
6 SettingElementCollection d = config.Settings;
7   if ( d != null )
8 foreach ( SettingElement de in d )
9 {
10 Console.WriteLine( " n={0} " , de.Name );
11 Console.WriteLine( " serializeAs={0} " , de.SerializeAs );
12 Console.WriteLine( " v={0} " , de.Value.ValueXml.InnerText );
13 }

注意,上述代码中需要引用System.Configuration.dll,因为此dll常规下不会自动添加

第二种,读取xml内容,需要再次读取。这种方式个人不建议。

    ConfigurationSection sectionV;
  
    // Get the current configuration file.
    System.Configuration.Configuration config =
     ConfigurationManager.OpenExeConfiguration(
     ConfigurationUserLevel.None) as Configuration;
  
    sectionV =
     config.GetSection("userSettings/SetConfig.Properties.Settings") as

ConfigurationSection ;
    
    Console.WriteLine("Section name: {0},raw:{1}",
     sectionV.SectionInformation.Name ,sectionV.SectionInformation.GetRawXml() );

关于自己写的这两类读取,本人赞同第一种,方便,直接获取节点值;第二种就在于再次分析xml,也许直接呈现是有用的。

而《.Net 自定义应用程序配置》所写的,也是好东西。建议使用。

总结:

关于自己写的这两类读取,本人赞同第一种,方便,直接获取节点值;

建议的另一点:还是appSettings读取。

最后说明一点:
无论哪类读取,目的都是为了获取配置;从根本上来说:就是xml的操作。这一点才是根本的。

喜欢用appSettings还是用它吧,简单方便。

至于2.0推荐的方式(msdn)也是好用,让用户自己继承接口,自己写读取,实用,只是代码多了点哦,呵呵

2011-4-11

今天在读取XML数据的时候,被ReadStartElement之类的函数迷惑了~杯具

其实跟遍历IDataReader一样,采用While(Reader.Read())处理。

好了,参看一段代码

 
  
1 while ( reader.Read() )
2 {
3 switch ( reader.NodeType )
4 {
5 case XmlNodeType.Element:
6 Console.Write( " <{0}> " , reader.Name );
7 break ;
8 case XmlNodeType.Text:
9 Console.Write( reader.Value );
10 break ;
11 case XmlNodeType.CDATA:
12 Console.Write( " <![CDATA[{0}]]> " , reader.Value );
13 break ;
14 case XmlNodeType.ProcessingInstruction:
15 Console.Write( " <?{0} {1}?> " , reader.Name, reader.Value );
16 break ;
17 case XmlNodeType.Comment:
18 Console.Write( " <!--{0}--> " , reader.Value );
19 break ;
20 case XmlNodeType.XmlDeclaration:
21 Console.Write( " <?xml version='1.0'?> " );
22 break ;
23 case XmlNodeType.Document:
24 break ;
25 case XmlNodeType.DocumentType:
26 Console.Write( " <!DOCTYPE {0} [{1}] " , reader.Name, reader.Value );
27 break ;
28 case XmlNodeType.EntityReference:
29 Console.Write( reader.Name );
30 break ;
31 case XmlNodeType.EndElement:
32 Console.Write( " </{0}> " , reader.Name );
33 break ;
34 }
35 }

在自己代码中应用:

using( XmlReader reader = XmlReader.Create( xmlfile ) )
{
    while(reader.Read())
    {
        if( reader.Name == "UserIDs" )
        {
            tmp = reader.ReadString();
            string[] uids = tmp.Split( ',' );
            foreach( string s in uids )
            {
                _UserIDs.Save( int.Parse( s ) );
            }
        }
        else if( reader.Name == "Item" )
        {
            _Contents.Save( reader.ReadString() );
        }
    }
}
PS:看得出来,下面这个是根据实际的需要来处理的,没有这么多switch了

转载于:https://www.cnblogs.com/GoGoagg/archive/2008/08/19/1271000.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值