我是 ASP.NET 的新手,
我正在制作国家,州下拉列表。
例如:对于特定国家,我将从 XML 文件中读取该国家的状态。
我无法在我的下拉列表中获取所需国家/地区的状态...
这是我的代码片段XMLFile.xml
<?xml version="1.0" encoding="utf-8" ?>
<countrys>
<country name="India">
<state value1="Maharashtra"></state>
<state value2="Kashmir"></state>
<state value3="Goa"></state>
</country>
<country name="Sri Lanka">
<state value1="Kanady"></state>
<state value2="Colombo"></state>
<state value3="Galle"></state>
</country>
<country name="Australia">
<state valu1e="Sydney"></state>
<state value2="Perth"></state>
<state value3="Melbourne"></state>
</country>
<country name="South Africa">
<state value1="Capetown"></state>
<state value2="Johanusburg"></state>
<state value3="Durban"></state>
</country>
</countrys>
和代码Country.aspx.cs
public partial class Country : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadDropdown();
}
}
protected void LoadDropdown()
{
DataSet ds = new DataSet();
ds.ReadXml (Server.MapPath("XMLFile.xml"));
DropDownListCountry.DataTextField = "country_text";
DropDownListCountry.DataSource = ds;
DropDownListCountry.DataBind();
DropDownListCountry.Items.Insert(0,new ListItem(" Select ","0"));
}
}
protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e)
{
string st = (DropDownListCountry.SelectedIndex).ToString();
XDocument main = XDocument.Load(@"XMLFile.xml");
var query = from state in doc.Descendants("countrys").Elements("country")
where st == state.Value
select state.NextNode;
DropDownListState.DataSource = query;
DropDownListState.DataBind();
}
}
错误: 对象引用未设置为对象的实例。
提前致谢 !!