IT人
相关图书

实现从XML文件中读取数据绑定到DropDownList的方法

编辑: 文章来源:网络 发布日期:2007-6-1 人气:

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:\Rhombussolution\Rhombus2\Languages.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>

相关文章
    网友对“实现从XML文件中读取数据绑定到DropDownList的方法”的评论
    已有位网友对本文发表评论,下面显示最近10条评论。 查看所有评论
    昵称:
    评论内容:
    Copyright ◎ 1998 - 2007 编程资料网 All Rights Reserved