0
private void BindCountry()
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("countries.xml"));
foreach (XmlNode node in doc.SelectNodes("//country"))
{
usrlocationddl.Items.Add(new ListItem(node.InnerText, node.Attributes["codes"].InnerText));
}
}
上述代码用于将xml文件中的国家列表加载到下拉列表中。但是,当这样做遇到空引用错误。将XML文件中的内容加载到下拉列表中时出错
对象引用未设置为对象的一个实例 。
内容的XML文件:
<countries>
<country code="AF" iso="4">Afghanistan</country>
<country code="AL" iso="8">Albania</country>
</countries>
在代码中,我应该改变,这样我可以逃避错误。
你可能想尝试使用Value属性代替InnerText属性的代码属性? – Kane 2010-04-05 07:38:09