Asp.net implements the method of binding XML files to DropDownList to read data, xmldropdownlist
The example in this article describes how to bind data from XML files to DropDownList using asp.net. We will share this with you for your reference. The details are as follows:
1. Bind DropDownList:
ddl_language.DataSource = createDataSource();ddl_language.DataTextField = "languageTextField";ddl_language.DataValueField = "languageValueField";ddl_language.DataBind();
2. The createDataSource () method used above:
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. Ages. xml
<? Xmlversion = "1.0" encoding = "UTF-8"?> <Shortagetypes> <language> <shortagevaluefield> en-US </shortagevaluefield> <shortagetextfield> English </shortagetextfield> </language> <shortagevaluefield> zh-CN
PS: Here are some online tools for xml operations for your reference:
Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson
Online formatting XML/online compression XML:
Http://tools.jb51.net/code/xmlformat
XMLOnline compression/formatting tools:
Http://tools.jb51.net/code/xml_format_compress
XMLCode Online formatting and beautification tools:
Http://tools.jb51.net/code/xmlcodeformat