且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将XML数据绑定到Dropdownlist C#

更新时间:2022-05-08 01:59:30

您可以将LINQ查询更改为以下内容,该查询将返回root下的所有节点,并返回具有您尝试的值/文本对的新项目.绑定.

You can change your LINQ query to the following which will return all the nodes under root and return new items with the value/text pairs that you are trying to bind to.

var query = from xEle in xDoc.Descendants()
select new {value = xEle.Attribute("value").Value , text = xEle.Attribute("text").Value};

然后按照以下步骤设置绑定,包括将查询折叠到列表中.

Then set up your bindings as follows including collapsing the query to a list.

ddlList.DataValueField = "value";
ddlList.DataTextField = "text";
ddlList.DataSource = query.ToList();
ddlList.DataBind();