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()方法:
privateICollectioncreateDataSource() { //createadatatabletostorethedatafortheddl_langaugecontrol DataTabledt=newDataTable(); //definethecolumnsofthetable dt.Columns.Add("languageTextField",typeof(string)); dt.Columns.Add("languageValueField",typeof(string)); //readthecontentofthexmlfileintoaDataSet DataSetlanDS=newDataSet(); stringfilePath=ConfigurationSettings.AppSettings["LanguageXmlFile"]; lanDS.ReadXml(filePath); if(lanDS.Tables.Count>0) { foreach(DataRowcopyRowinlanDS.Tables[0].Rows) { dt.ImportRow(copyRow); } } DataViewdv=newDataView(dt); returndv; }
3、Web.config
<appSettings> <!--Thefilepathforthelanguagetypexmlfile--> <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操作的在线工具供大家参考使用:
在线