XML文件读取数据绑定到DropDownList

ZDNet软件频道 时间:2008-07-05 作者:hufengwe… | 中国IT实验室 我要评论()
本文关键词:DropDownList 绑定 xml 软件
XML文件读取数据绑定到DropDownList

1 、绑定DropDownList

    ddl_language.DataSource = createDataSource();
    ddl_language.DataTextField = "languageTextField";
    ddl_language.DataValueField = "languageValueField";
    ddl_language.DataBind();

2、上面用到的createDataSource()方法: 

           private ICollection createDataSource()
            {
                  //create a data table to store the data for the ddl_langauge control
                  DataTable dt = new DataTable();
 
                  //define the columns of the table
                  dt.Columns.Add("languageTextField",typeof(string));
                  dt.Columns.Add("languageValueField",typeof(string));
 
                  //read the content of the xml file into a DataSet
                  DataSet lanDS = new DataSet();
                  string filePath = ConfigurationSettings.AppSettings["LanguagexmlFile"];
                  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;
            }
3、Web.config
 
        <appSettings>
            <!--The file path for the language type xml file-->
            <addkey="LanguagexmlFile"value="d:RhombussolutionRhombus2Languages.xml"/>
</appSettings>

 4、Languages.xml 

<?xmlversion="1.0"encoding="utf-8"?>
<languageTypes>
   <language>
      <languageValueField>en-US</languageValueField>
      <languageTextField>English</languageTextField>
   </language>
   <language>
      <languageValueField>zh-CN</languageValueField>
      <languageTextField>中文</languageTextField>
   </language>
   <language>
      <languageValueField>ja-JP</languageValueField>
      <languageTextField>日语</languageTextField>
   </language>
</languageTypes>

DropDownList

绑定

xml

软件


百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134