从xml文件中绑定数据到DropDownList控件上

时间:2023-03-09 15:38:16
从xml文件中绑定数据到DropDownList控件上

参考了2篇文章:

http://www.cnblogs.com/JuneZhang/archive/2010/11/23/1885671.html

http://blog.sina.com.cn/s/blog_4d4d3ade01000apj.html

    private string _RootPath;
/// <summary>
/// 系统的根目录
/// </summary>
public string RootPath
{
get
{ _RootPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath).ToLower();//当前的绝对路径
if (_RootPath.Length == )
{
_RootPath = "";
}
return _RootPath;
}
}
  private void BindDepConfig()
{
dropdownDep.DataSource = createDataSource();
dropdownDep.DataTextField = "depTextField";
dropdownDep.DataValueField = "depValueField";
dropdownDep.DataBind(); } private ICollection createDataSource() { DataTable dt = new DataTable(); //define the columns of the table
dt.Columns.Add("depTextField",typeof(string));
dt.Columns.Add("depValueField",typeof(string)); //read the content of the xml file into a DataSet
DataSet lanDS = new DataSet();
string filePath = RootPath + "/Data/Xml/ThridDepConfig.xml";
lanDS.ReadXml(filePath); if(lanDS.Tables.Count > )
{
foreach(DataRow copyRow in lanDS.Tables[].Rows)
{
dt.ImportRow(copyRow);
}
} DataView dv = new DataView(dt); return dv;
}
<?xml version="1.0" encoding="utf-8"?>

<depTypes>
<dep>
<depValueField>电教处</depValueField>
<depTextField>电教处</depTextField>
</dep>
<dep>
<depValueField>总务处</depValueField>
<depTextField>总务处</depTextField>
</dep>
<dep>
<depValueField>后勤处</depValueField>
<depTextField>后勤处</depTextField>
</dep>
</depTypes>