dinghao

记录成长点滴

 

通过例子看CS的配置文件,把内部类作为HashTable成员

先建立一个明为:Login.Config的配置文件

?xml version="1.0" encoding="utf-8" ?> 
<login>
    
<oems>
        
<add key="{056CAAC6-AA9A-4748-883E-7125086CAD06}" name = "ttt" 
            min
="7000010001" max ="7000019999"
            domain
="gtalkie.com"
            connectionString
="DSN=SerMySQL;Uid=engqi;Pwd=123456;"
            
/>
            
<add key="{CEEB5601-FF2C-4A0D-BA2B-7812D77DFE73}" name = "aaa" 
            min
="6000300000" max ="6000399999"
            domain
="cc.com"
            connectionString
="DSN=qinghua;Uid=xbj;Pwd=123456;"
            
/>
    
</oems>
</login>
读取配置文件的类
public class LoginConfig
    
{
        
public static readonly string CacheKey = "LoginConfiguration";
        Hashtable oems 
= new Hashtable();
        
private int cacheFactor = 5;
        
private XmlDocument XmlDoc = null;
        
public LoginConfig(XmlDocument doc)
        
{
            XmlDoc 
= doc;
            LoadValuesFromConfigurationXml();
        }


        
public XmlNode GetConfigSection(string nodePath)
        
{
            
return XmlDoc.SelectSingleNode(nodePath);
        }


        
public Hashtable Oems
        
{
            
get
            
{
                
return oems;
            }

        }

        
public static LoginConfig GetConfig() 
        
{
            LoginConfig config 
= CSCache.Get(CacheKey) as LoginConfig;
            
if(config == null)
            
{
                
string path = null;
                HttpContext context 
= HttpContext.Current;
                
if(context != null)
                    path 
= context.Server.MapPath("~/login.config");
                
else
                    path 
= Path.Combine(AppDomain.CurrentDomain.BaseDirectory,  "login.config");

                XmlDocument doc 
= new XmlDocument();
                doc.Load(path);
                config 
= new LoginConfig(doc);
                CSCache.Max(CacheKey,config,
new CacheDependency(path));

                CSCache.ReSetFactor(config.CacheFactor);
            }

            
return config;
            
        }


        
internal void LoadValuesFromConfigurationXml() 
        
{
           
            
//todocachefactor
            XmlNode node = GetConfigSection("login");
            
            
foreach (XmlNode child in node.ChildNodes) 
            
{

                
if (child.Name == "oems")
                    GetOems(child, oems);

                

            }


        

        }


        
internal void GetOems(XmlNode node, Hashtable table) 
        
{

            
foreach (XmlNode oem in node.ChildNodes) 
            
{

                
switch (oem.Name) 
                
{
                    
case "add" :
                        table.Add(oem.Attributes[
"key"].Value, new Oem(oem.Attributes) );
                        
break;

                    
case "remove" :
                        table.Remove(oem.Attributes[
"key"].Value);
                        
break;

                    
case "clear" :
                        table.Clear();
                        
break;

                }


            }


        }

        
public int CacheFactor
        
{
             
get  {  return cacheFactor;  } 
        }


        
public class Oem 
        
{
            
            
string key;

            NameValueCollection providerAttributes 
= new NameValueCollection();

            
public Oem (XmlAttributeCollection attributes) 
            
{

                
                
                key 
= attributes["key"].Value;

                
// Store all the attributes in the attributes bucket
                
//
                foreach (XmlAttribute attribute in attributes) 
                
{
                    
if ( (attribute.Name != "key") )
                        providerAttributes.Add(attribute.Name, attribute.Value);
                }

            }

        
            
public string Key 
            
{
                
get 
                
{
                    
return key;
                }

            }


            
        

            
public NameValueCollection Attributes 
            
{
                
get 
                
{
                    
return providerAttributes;
                }

            }

            
        }


    }

login.config文件映射Oem内部类 。节点的属性映射到Attributes
重要的地方:把内部类放到了Hashtable。
Loginconfig 的Oem属性为Hashtable类型,其中的值则是Oem类的实例,这样就可以通过Oem属性访问Oem内部类了。
缺点是HashTable不能被序列化,用到序列化时可以自己写类型或者用IList

posted on 2006-05-25 12:53  思无邪  阅读(1098)  评论(1编辑  收藏  举报

导航