我想做一个Country ComboBox ,我如何将此XML文件绑定到ComboBox,这是我的代码:
public class CountriesComboBox : ComboBox
{
public CountriesComboBox()
{
XDocument obj = XDocument.Load("countries.xml");
//DisplayMember = "countryiso";
//ValueMember = "countrycode";
DataSource = obj.Descendants("country").Select(x => new
{
countrycode = x.Attribute("code").Value,
countryiso = x.Attribute("name").Value
}).ToList();
}
}
这是我的 XML 文件:
<countries>
<country code="AF" iso="4">Afghanistan</country>
<country code="AL" iso="8">Albania</country>
<country code="DZ" iso="12">Algeria</country>
<country code="AS" iso="16">American Samoa</country>
<country code="AD" iso="20">Andorra</country>
<country code="AO" iso="24">Angola</country> ....