从XML文件中读取数据绑定到DropDownList
2008-02-19 17:04
555 查看
using System.Xml;
using System.Collections;
1 、绑定DropDownList:
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.DataSource = createDataSource();
DropDownList1.DataTextField = "languageTextField";
DropDownList1.DataValueField = "languageValueField";
DropDownList1.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 = Server.MapPath(ConfigurationManager.AppSettings["LanguageXmlFile"].ToString());
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
<configuration>
<appSettings>
<!--The file path for the language type xml file-->
<add key="LanguageXmlFile" value="Languages.xml"/>
</appSettings>
<connectionStrings/>
4、Languages.xml
<?xml version="1.0" encoding="gb2312"?>
<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>
code down
using System.Collections;
1 、绑定DropDownList:
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.DataSource = createDataSource();
DropDownList1.DataTextField = "languageTextField";
DropDownList1.DataValueField = "languageValueField";
DropDownList1.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 = Server.MapPath(ConfigurationManager.AppSettings["LanguageXmlFile"].ToString());
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
<configuration>
<appSettings>
<!--The file path for the language type xml file-->
<add key="LanguageXmlFile" value="Languages.xml"/>
</appSettings>
<connectionStrings/>
4、Languages.xml
<?xml version="1.0" encoding="gb2312"?>
<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>
code down
相关文章推荐
- 从XML文件中读取数据绑定到DropDownList
- XML文件读取数据绑定到DropDownList
- 如何从XML文件中读取数据绑定到DropDownList
- 转载--从XML文件中读取数据绑定到DropDownList
- 本文介绍了如何从XML文件中读取数据绑定到DropDownList。
- 从XML文件中读取数据绑定到DropDownList
- 从XML文件中读取数据绑定到DropDownList
- 转载--从XML文件中读取数据绑定到DropDownList
- 从XML文件中读取数据绑定到DropDownList-.Net编程教程
- Demo: Dictionary>> 泛型读取XML文件数据作为数据源(DataSource)绑定到DropDownList控件 定义个一个公用类
- Dropdownlist统一配置(从xml或枚举读取数据),应用文件依赖缓存.
- 从xml文件中绑定数据到DropDownList控件上
- asp.net 读取xml文件里面的内容,绑定到dropdownlist中
- 一个用ArrayCollection作为数据格式 读取远程XML绑定到DropDownList例子
- ZendFramework学习第三章(核心组件—使用配置数据之从XML配置文件中读取数据)
- JS读取XML文件数据并以table形式显示数据的方法(兼容IE与火狐)
- 利用缓存机制快速读取XML文件数据
- Windows Phone 7的XML操作详解:读取,过滤以及数据绑定
- WP7的XML操作详解:读取,过滤以及数据绑定
- WP7 XML操作详解:读取,过滤以及数据绑定