从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 == 1)
                    {
                        _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 > 0)
                  {
                              foreach(DataRow copyRow in lanDS.Tables[0].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>

 

 

 

posted on 2013-09-23 16:11  蓝蓝的天2016  阅读(685)  评论(1编辑  收藏  举报