asp. net Implementation of XML File Read Data Binding to DropDownList
- 2021-09-11 19:55:15
- OfStack
This article describes the example of asp. net implementation of XML file read data binding to DropDownList method. Share it for your reference, 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. Languages. xml
<?xmlversion="1.0"encoding="utf-8"?>
<languageTypes>
<language>
<languageValueField>en-US</languageValueField>
<languageTextField>English</languageTextField>
</language>
<language>
<languageValueField>zh-CN</languageValueField>
<languageTextField> Chinese </languageTextField>
</language>
<language>
<languageValueField>ja-JP</languageValueField>
<languageTextField> Japanese </languageTextField>
</language>
</languageTypes>
PS: Here are several online tools for xml operation for your reference:
Online XML/JSON Interconversion Tool:
http://tools.ofstack.com/code/xmljson
Online Formatting XML/Online Compressing XML:
http://tools.ofstack.com/code/xmlformat
XML Online Compression/Formatting Tools:
http://tools.ofstack.com/code/xml_format_compress
XML code online formatting beautification tool:
http://tools.ofstack.com/code/xmlcodeformat
For more readers interested in asp. net related contents, please check the topics of this site: "asp.net Operation XML Skills Summary", "asp.net Operation json Skills Summary", "asp.net String Operation Skills Summary", "asp. net File Operation Skills Summary", "asp. net ajax Skills Summary" and "asp. net Cache Operation Skills Summary".
I hope this paper is helpful to everyone's asp. net programming.