Binding XML to Combobox
While trying to bind XML to Combobox data source, I am getting an error "Complex DataBinding accepts as a data source either an IList or an IListSource."
XDocument obj = XDocument.Load("Managers.xml");
comboBox1.DisplayMember = "ManagerDesig";
comboBox1.ValueMember = "ManagerID";
comboBox1.DataSource = obj.Descendants("manager").Select(x => new
{
ManagerDesig = x.Attribute("desig").Value,
ManagerID = x.Attribute("id").Value
});
Managers.xml
<managers>
<manager id="123" desig="CEO" />
<man开发者_C百科ager id="234" desig="CFO" />
<manager id="456" desig="CIO" />
</managers>
Please help
try:
comboBox1.DataSource = obj.Descendants("manager").Select(x => new
{
ManagerDesig = x.Attribute("name").Value,
ManagerID = x.Attribute("id").Value
})
.ToList();//convert to list
First, there is a typo in your example: you're retrieving the "name" attribute whereas there are only "id" and "desig" attributes defined.
Second, use ToList extension method like the following:
comboBox1.DataSource = obj.Descendants("manager").Select(x => new
{
ManagerDesig = x.Attribute("name").Value,
ManagerID = x.Attribute("id").Value
}).ToList();
精彩评论