当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net 读取xml文件里面的内容,绑定到dropdownlist中

ASP.NET
asp.net 产生随机颜色实现代码
asp.ent(C#)中判断空字符串的3种方法以及性能分析
asp.net 基于forms验证的目录角色权限的实现
ASP.NET 统计图表控件小结
asp.net 动态引用样式表代码
asp.net 获取IP的相关资料
真正的获取客户端真实IP地址及利弊分析
asp.net(c#)文件下载实现代码
asp.net 不用GridView自带删除功能,删除一行数据
asp.net forms身份验证,避免重复造轮子
asp.net 站点URLRewrite使用小记
asp.net Gridview行绑定事件新体会
asp.net MVC实现简单的上传功能
asp.net web.config加密解密方法
aspx实现的 jquery ui 的 flexgrid demo
ASP.NET Internet安全Forms身份验证方法
asp.net使用for循环实现Datalist的分列显示功能
jQuery AJax调用asp.net webservers的实现代码
ASP.NET 页面刷新和定时跳转代码整理
asp.net GridView控件鼠标移动某行改变背景颜色(方法一)

ASP.NET 中的 asp.net 读取xml文件里面的内容,绑定到dropdownlist中


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 209 ::
收藏到网摘: n/a

asp.net 读取xml文件里面的内容,绑定到dropdownlist中的实现代码。 xml文件编写
复制代码 代码如下:

<?xml version="1.0" encoding="gb2312" ?>
<BookType>
<parameter>
<name>商务管理</name>
<value>0</value>
</parameter>
<parameter>
<name>金融管理</name>
<value>1</value>
</parameter>
<parameter>
<name>心理学专业</name>
<value>2</value>
</parameter>
<parameter>
<name>心理咨询师</name>
<value>3</value>
</parameter>
<parameter>
<name>企业行政管理师</name>
<value>4</value>
</parameter>
</BookType>
.aspx页面
<tr bgcolor="#ffffff">
<td>
专业:</td>
<td>
<asp:DropDownList ID="y_zhuanye" runat="server" Width="144px">
</asp:DropDownList></td>
</tr>

.aspx.cs页面
复制代码 代码如下:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
fileName = Server.MapPath("ZhuangYe.xml");
this.SetDropDownList(fileName, y_zhuanye);
this.DataBind();
}
else
{
}
}
//读取XML里的信息
//fileName 表示要读取的XML文件名的路径
//listBox 表示要添加在那个DropDownList 下拉框里
public void SetDropDownList(String fileName, DropDownList listBox)
{
//String fileName = Server.MapPath("BookType.xml");
XmlTextReader myXMLReader = new XmlTextReader(fileName);
String tempName="";
while (myXMLReader.Read())
{
if (myXMLReader.NodeType == XmlNodeType.Element)
{
if (myXMLReader.LocalName.Equals("name"))
{
tempName =myXMLReader.ReadString();
}
else if (myXMLReader.LocalName.Equals("value"))
{
String tempValues = myXMLReader.ReadString();
if (tempName == null || tempName.Equals(""))
{
}
else
{
listBox.Items.Add(new ListItem(tempName,tempValues));
}
}
else
{
}
}
else
{
}
}
}