xml内容:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<column value="1">单项选择题</column>
<column value="2">多项选择题</column>
<column value="3">判断题</column>
<column value="4">论述题</column>
<column value="5">案例分析</column>
<column value="6">面试题</column>
</root>
页面存放一个dropdownlist
<asp:DropDownList ID="ddlsttype" runat="server"> </asp:DropDownList>
。cs代码:
方法:
/// <summary>
/// 绑定试题类型
/// </summary>
private void bindExamType()
{
StringBuilder linklist = new StringBuilder();
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("../data/examtype.xml"));
XmlElement root = xmldoc.DocumentElement;
#region xml绑定dropdownlist(步骤一)
DataTable MyDt;
//创建表头Columns
string str = "";
MyDt = new DataTable();
MyDt.Columns.Add(new DataColumn("code", str.GetType()));//编号
MyDt.Columns.Add(new DataColumn("name", str.GetType()));//名称
#endregion
foreach (XmlNode node in root.ChildNodes)
{
#region xml绑定dropdownlist(步骤二)
DataRow MyDr = MyDt.NewRow();
MyDr[0] = node.Attributes["value"].Value;
MyDr[1] = node.InnerText;
MyDt.Rows.Add(MyDr);
#endregion
linklist.Append(string.Format("<a href='QuestionList.aspx?type={0}'>{1}</a> ", node.Attributes["value"].Value, node.InnerText));
}
this.examtypelist = linklist.ToString();
#region xml绑定dropdownlist(步骤三)
ddlsttype.DataTextField = "name";
ddlsttype.DataValueField = "code";
ddlsttype.DataSource = MyDt;
ddlsttype.DataBind();
ddlsttype.Items.Insert(0, new ListItem("请选择", "-1"));
#endregion
}