Demo: Dictionary>> 泛型读取XML文件数据作为数据源(DataSource)绑定到DropDownList控件 定义个一个公用类

1true_suffix_name.DataSource = Common.OptionDictionary["suffix"];
2            true_suffix_name.DataTextField = "key";
3            true_suffix_name.DataValueField = "value";

 

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.IO;
using ASPSOFT.Utility;
using System.Collections.Generic;
using System.Collections;
using System.Xml;

/// <summary>
/// Summary description for Common
/// </summary>

public class Common
{
    
public Common()
    
{
        
//
        
// TODO: Add constructor logic here
        
//
    }


    
private static Dictionary<string, List<KeyValuePair<stringstring>>> _optionDictionary; //= new Dictionary<string, List<KeyValuePair<string, string>>>();

    
public static Dictionary<string, List<KeyValuePair<stringstring>>> OptionDictionary
    
{
        
get {
            
if (null == _optionDictionary)
                _optionDictionary 
= new Dictionary<string, List<KeyValuePair<stringstring>>>();

            
if (_optionDictionary.Count == 0)// 0
            {
                
string strXMLFile = HttpContext.Current.Server.MapPath("~/TCR_SQL.xml");
                
if (File.Exists(strXMLFile)) // file
                {
                    
//read sql clause from the xml file (suffix,race,sex,state,xref_no etc)
                    XmlDocument xmldoc = new XmlDocument();
                    
try
                    
{
                        xmldoc.Load(strXMLFile);
                        XmlNodeList sqlNodeList 
= xmldoc.SelectSingleNode("collect").ChildNodes;

                        
foreach (XmlNode xn in sqlNodeList)
                        
{
                            XmlElement xe 
= (XmlElement)xn;
                            _optionDictionary.Add(xe.GetAttribute(
"item").ToLower(), GetEnumsFromTable(xe.InnerText));
                        }

                    }

                    
catch
                    
{ }

                    
// directly use sql string clause
                    string[][] sqlKeyValueList = new string[][]
                             
new string[]"JuvenileAdult","SELECT VARCHAR3, CODE FROM .."}
                            ,
new string[]"HairColor","select description, code from .."}
                            ,
new string[]"EyeColor","select description, code from .."}
                            ,
new string[]"CitizenshipCCLV","select code1=code, code from .."}
                            ,
new string[]"VAF","select code1=code, code from .."}
                            ,
new string[]"PD","select asso_name, asso_id  from .."}
                            ,
new string[]"YesNo","select description, code from .."}
                        }
;

                    
for (int i = 0; i < sqlKeyValueList.Length; i++)
                    
{
                        
try
                        
{
                            _optionDictionary.Add(sqlKeyValueList[i][
0].ToLower(), GetEnumsFromTable(sqlKeyValueList[i][1]));
                        }

                        
catch
                        
{ }
                    }

                }
//end file
                else
                
{
                    
throw new Exception("TCR_SQL.xml file doesn't exist");
                }

            }
//end 0
            return _optionDictionary;
        }
 // end get
    }


    
public static void AddOption(string key, string value)
    
{
        
if (OptionDictionary.ContainsKey(key))
        
{
            OptionDictionary[key].Add(
new KeyValuePair<stringstring>(value, value));
        }

        
else
        
{
            List
<KeyValuePair<stringstring>> objData = new List<KeyValuePair<stringstring>>();
            objData.Add(
new KeyValuePair<stringstring>(value, value));
            OptionDictionary.Add(key, objData);
        }

    }


    
private static List<KeyValuePair<stringstring>> GetEnumsFromTable(string strSql)
    
{
        List
<KeyValuePair<stringstring>> ht = new List<KeyValuePair<stringstring>>();

        ht.Add(
new KeyValuePair<stringstring>(""""));

        
if (!string.IsNullOrEmpty(strSql))
        
{
            DataTable dt 
= DALHelper.GetDataTable(strSql);
            
if (null != dt && dt.Columns.Count == 2)
            
{
                
foreach (DataRow row in dt.Rows)
                
{
                    
try
                    
{
                        ht.Add(
new KeyValuePair<stringstring>(row[0].ToString(), row[1].ToString()));
                    }

                    
catch 
                    
{ }
                }

            }

        }


        
return ht;
    }

}


XML 文件:
1<?xml version="1.0" ?>
2<collect>
3 <sql item="received_by">
4  select staff_name, staff_id from staff where (staff_type='INVESTIGATOR' or staff_type='AGENT') and flag_delete=0 order by staff_name
5 </sql>
6 <sql item="municipality_code">
7  select description, code from asp_lookup where lookup_id='municipality' and varchar10='DCOUNTY' and display = 'Y' order by description
8 </sql>
9</collect>
posted @ 2008-06-30 19:36  杰克帝.NET  阅读(1293)  评论(0编辑  收藏  举报