当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net实现XML文件读取数据绑定到DropDownList的方法

asp.net实现XML文件读取数据绑定到DropDownList的方法

2017年12月08日  | 移动技术网IT编程  | 我要评论

地骨皮的作用与功效,灵璧县教育局,绝密追杀

本文实例讲述了asp.net实现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:\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>

ps:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线xml/json互相转换工具:

在线格式化xml/在线压缩xml

xml在线压缩/格式化工具:

xml代码在线格式化美化工具:

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作xml技巧总结》、《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网