Asp.net implements the method of binding XML files to DropDownList to read data, xmldropdownlist

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.