zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    关于 a 标签 jquery的trigger("click"),无法触发问题。
    浏览器上传文件,存到oracle数据库示例。
    关于java的Long 类型到js丢失精度的问题
    java 自定义注解,并使用示例
    关于重置功能(type="reset")的相关问题
    校验键盘上中英文状态下所有的特殊字符(排除下划线所在的按键)
    VS Code 设置取消打开文件目录的自动定位跟踪功能。
    $.extend(x,y); 函数用法介绍。
    用jquery的.val() 给具有style="display:none;" 属性的标签写值的问题。
    10.我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。 请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
  • 原文地址:https://www.cnblogs.com/Dlonghow/p/1232683.html
Copyright © 2011-2022 走看看