<?xml version="1.0" encoding="UTF-8"?>
<root>
<CountryItem>
<Item Value="CN">中国</Item>
<Item Value="AM">美国</Item>
<Item Value="JP">日本</Item>
<Item Value="GE">德国</Item>
</CountryItem>
</root>
.DataTextField 绑定中文字
.DataValueField 绑定节点属性Value的值
如何写呢?
method 1:
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(Server.MapPath("YourXml.xml"));
foreach (System.Xml.XmlNode node in doc.SelectNodes("/root/CountryItem/Item"))
{
YourDropDown.Items.Add(new ListItem(node.InnerText,node.Attributes["Value"].Value));
}
method 2:
System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml(Server.MapPath("YourXml.xml"));
YourDropDown.DataSource = ds.Tables["Item"].DefaultView;
YourDropDown.DataTextField = "Item_Text";
YourDropDown.DataValueField = "Value";
YourDropDown.DataBind();