华域联盟 .Net asp.net实现XML文件读取数据绑定到DropDownList的方法

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

本文实例讲述了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互相转换工具:
http://tools.jb51.net/code/xmljson

在线格式化XML/在线压缩XML
http://tools.jb51.net/code/xmlformat

XML在线压缩/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代码在线格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

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

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

您可能感兴趣的文章:

  • asp.net 读取xml文件里面的内容,绑定到dropdownlist中
  • asp.net使用DataSet的ReadXml读取XML文件及Stream流的方法
  • ASP.NET读取XML文件4种方法分析
  • Javascript+XMLHttpRequest+asp.net无刷新读取数据库数据
  • ASP.NET中读取XML文件信息的4种方法与示例代码
  • ASP.NET MVC DropDownList数据绑定及使用详解
  • ASP.NET MVC中为DropDownListFor设置选中项的方法
  • ASP.NET Ajax级联DropDownList实现代码
  • asp.net DropDownList自定义控件,让你的分类更清晰
  • ASP.NET DropDownList控件的使用方法
  • (asp.net c#)DropDownList绑定后显示对应的项的两种方法
  • ASP.NET笔记之 ListView 与 DropDownList的使用

本文由 华域联盟 原创撰写:华域联盟 » asp.net实现XML文件读取数据绑定到DropDownList的方法

转载请保留出处和原文链接:https://www.cnhackhy.com/75419.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部