参考
In config:<Y>
<XConfiguration>
<add key="DataAccess.ConnectionString" value="user id=user;password=pwd;initial catalog=blub;data source=localhost" />
</XConfiguration>
</Y>
Then:public class XConfiguration :
System.Configuration.IConfigurationSectionHandler
{
public object Create(object parent, object input, XmlNode section)
{
System.Collections.Specialized.NameValueCollection config;
try
{
config =
(NameValueCollection)ConfigurationSettings.GetConfig("Y/XConfiguration");
}
catch
{
config = null;
}
if (config != null)
{
s_ConnectionString = config["DataAccess.ConnectionString"];
}
}
In config:<Y>
<XConfiguration>
<add key="DataAccess.ConnectionString" value="user id=user;password=pwd;initial catalog=blub;data source=localhost" />
</XConfiguration>
</Y>
Then:public class XConfiguration :
System.Configuration.IConfigurationSectionHandler
{
public object Create(object parent, object input, XmlNode section)
{
System.Collections.Specialized.NameValueCollection config;
try
{
config =
(NameValueCollection)ConfigurationSettings.GetConfig("Y/XConfiguration");
}
catch
{
config = null;
}
if (config != null)
{
s_ConnectionString = config["DataAccess.ConnectionString"];
}
}
解决方案 »
- 自定义控件中颜色属性在vs环境下预览的问题
- 分两列显示数据
- 怎么算出邮件的大小(急用)
- ASP.NET页面显示系统时间问题
- asp.net下的Sql server一个存储过程问题?
- 各位大哥:在asp.net里,通过linkbutton链接到其它的页面,我该怎么做?
- 实在不会我才问,请帮帮忙。最后20分了
- 电子邮箱的入口问题?form表单的问题?(ASP)急。。。在线等待
- 如何将winform控件添加到webform中?
- 请问ASP.net如何接收邮件?谢谢。
- 请教:为什么DataView.RowFilter里面不能使用类似“a_b_C_d”的格式
- 各位大虾救命,为何我做的.net系统在实施过程中出现不稳定现象,用户很有意见,我却找不出原因?
using System.Web;
using System.Xml;
using System.Configuration;namespace MyConfig
{
public enum LevelSetting
{
High,
Medium,
Low,
None
}
public class MyConfigSectionHandler : IConfigurationSectionHandler
{
public virtual object Create(object parent,object configContext,XmlNode section)
{
int iLevel = 0;
string sName = "";
ConfigHelper.GetEnumValue(section, "level", typeof(LevelSetting), ref iLevel);
ConfigHelper.GetStringValue(section,"name",ref sName);
return new MyConfigSection((LevelSetting)iLevel,sName);
}
}
public class MyConfigSection
{
private LevelSetting level = LevelSetting.None;
private string name = null;
public MyConfigSection(LevelSetting _level,string _name)
{
level = _level;
name = _name;
}
public LevelSetting Level
{
get {return level;}
}
public string Name
{
get {return name;}
}
}
internal class ConfigHelper
{
public static XmlNode GetEnumValue
(XmlNode _node, string _attribute,Type _enumType, ref int _val)
{
XmlNode a = _node.Attributes.RemoveNamedItem(_attribute);
if(a==null)
throw new ConfigurationException("Attribute required: " + _attribute);
if(Enum.IsDefined(_enumType, a.Value))
_val = (int)Enum.Parse(_enumType,a.Value);
else
throw new ConfigurationException("Invalid Level",a);
return a;
}
public static XmlNode GetStringValue(XmlNode _node, string _attribute, ref string _val)
{
XmlNode a = _node.Attributes.RemoveNamedItem(_attribute);
if(a==null)
throw new ConfigurationException("Attribute required: " + _attribute);
else
_val = a.Value;
return a;
}
}
}
在Web.config增加:<configSections>
<sectionGroup name="system.web">
<section name="myConfig" type="MyConfig.MyConfigSectionHandler,MyConfig" />
</sectionGroup>
</configSections>
<system.web>
<myConfig level="High" name="hello world" />
</system.web>WebForm1.aspx.cs:using MyConfig;MyConfigSection s = (MyConfigSection)Context.GetConfig("system.web/myConfig");
Response.Write("Level: " + s.Level + "<br>");
Response.Write("Name: " + s.Name);