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>
  • 相关阅读:
    atitit.nfc 身份证 银行卡 芯片卡 解决方案 attilax总结
    atitit.php 流行框架 前三甲为:Laravel、Phalcon、Symfony2 attilax 总结
    Atitit.执行cmd 命令行 php
    Atitit. 图像处理jpg图片的压缩 清理垃圾图片 java版本
    atitit。企业组织与软件工程的策略 战略 趋势 原则 attilax 大总结
    atitit. 管理哲学 大毁灭 如何防止企业的自我毁灭
    Atitit.java的浏览器插件技术 Applet japplet attilax总结
    Atitit.jquery 版本新特性attilax总结
    Atitit. 软件开发中的管理哲学一个伟大的事业必然是过程导向为主 过程导向 vs 结果导向
    (转)获取手机的IMEI号
  • 原文地址:https://www.cnblogs.com/Dlonghow/p/1232683.html
Copyright © 2011-2022 走看看