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> publicclass Common { public Common() { // // TODO: Add constructor logic here // } privatestatic Dictionary<string, List<KeyValuePair<string, string>>> _optionDictionary; //= new Dictionary<string, List<KeyValuePair<string, string>>>(); publicstatic Dictionary<string, List<KeyValuePair<string, string>>> OptionDictionary { get{ if (null== _optionDictionary) _optionDictionary =new Dictionary<string, List<KeyValuePair<string, string>>>(); 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 =newstring[][]{ newstring[]{ "JuvenileAdult","SELECT VARCHAR3, CODE FROM .."} ,newstring[]{ "HairColor","select description, code from .."} ,newstring[]{ "EyeColor","select description, code from .."} ,newstring[]{ "CitizenshipCCLV","select code1=code, code from .."} ,newstring[]{ "VAF","select code1=code, code from .."} ,newstring[]{ "PD","select asso_name, asso_id from .."} ,newstring[]{ "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 { thrownew Exception("TCR_SQL.xml file doesn't exist"); } }//end 0 return _optionDictionary; }// end get } publicstaticvoid AddOption(string key, string value) { if (OptionDictionary.ContainsKey(key)) { OptionDictionary[key].Add(new KeyValuePair<string, string>(value, value)); } else { List<KeyValuePair<string, string>> objData =new List<KeyValuePair<string, string>>(); objData.Add(new KeyValuePair<string, string>(value, value)); OptionDictionary.Add(key, objData); } } privatestatic List<KeyValuePair<string, string>> GetEnumsFromTable(string strSql) { List<KeyValuePair<string, string>> ht =new List<KeyValuePair<string, string>>(); ht.Add(new KeyValuePair<string, string>("", "")); 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<string, string>(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>