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

xml文件编写
复制代码 代码如下:




商务管理
0


金融管理
1


心理学专业
2


心理咨询师
3


企业行政管理师
4


.aspx页面


专业:





.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
{
}
}
}

你可能感兴趣的:(asp.net 读取xml文件里面的内容,绑定到dropdownlist中)