从XML文件中读取数据绑定到DropDownList

1 、绑定DropDownList:

   

以下为引用的内容:
ddl_language.DataSource = createDataSource();
    ddl_language.DataTextField = "languageTextField";
    ddl_language.DataValueField = "languageValueField";
    ddl_language.DataBind();

字串2

 

字串3

 

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

字串7

 

字串2

 

以下为引用的内容:
 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)); 字串7
                  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)
                  {

字串6


                              foreach(DataRow copyRow in lanDS.Tables[0].Rows)
                              {
                                    dt.ImportRow(copyRow);
                            }
                  }
 
                  DataView dv = new DataView(dt); 字串2
 
                  return dv;
            }
字串1

 

3、Web.config
        

字串5

 

以下为引用的内容:
<appSettings>
            <!--The file path for the language type xml file-->
            <addkey="LanguageXmlFile"value="d:\Rhombussolution\Rhombus2\Languages.xml"/>
</appSettings>
字串5

 4、Languages.xml 

字串5

字串4

 

以下为引用的内容:

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

字串4

posted @ 2007-09-16 19:15  星空竹月  阅读(184)  评论(0编辑  收藏  举报