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

本文介绍了一种使用C#从XML文件和SQL查询中动态获取数据来填充ASP.NET DropDownList的方法。通过解析XML文件中的SQL语句或直接使用预定义的SQL语句,此方法能够有效地为应用程序提供配置选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 true_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 >

转载于:https://www.cnblogs.com/Dlonghow/archive/2008/06/30/1232683.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值