public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//if (!ispostback)
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("E:/UseListManage/xmlDataList.xml");
string str = DropDownList1.SelectedValue;
//根据ClassID号选中节点
XmlNode node = doc.SelectSingleNode("//Table[@ClassID='"+ str +"']");
noteAdd(node);
}
/// <summary>
/// 在dropdownlist中增加该节点属性,遍历其子节点,递归实现
/// </summary>
/// <param name="node"></param>
protected void noteAdd(XmlNode node)
{
itemsAdd(node);
if (node.ChildNodes.Count != 0)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
noteAdd(node.ChildNodes[i]);
}
}
}
/// <summary>
/// 在dropdownlist中增加一个节点的所有属性
/// </summary>
/// <param name="nextNode"></param>
protected void itemsAdd(XmlNode nextNode)
{
for (int j = 0; j < nextNode.Attributes.Count; j++)
{
DropDownList2.Items.Add(new ListItem(nextNode.Attributes[j].Name, nextNode.Attributes[j].Value));
}
}
/// <summary>
/// 选中属性后赋值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
txtValue.Text = DropDownList2.SelectedValue;
}
}
总结:先实例化一个Xmldocument对象,利用该对象加载xml文件 并利用节点标记信息ClassID找到节点开始遍历。