将XML数据绑定到Dropdownlist C# [英] Bind XML data to a Dropdownlist c#

查看:163
本文介绍了将XML数据绑定到Dropdownlist C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将XML数据绑定到Dropdownlist

I am trying to bind XML data to Dropdownlist

 XElement xDoc = XElement.Parse(QContent.OuterXml);

这是我的xDoc包含的内容

Here is what my xDoc contains

<root xmlns="">
       <item value="-1" text="Select" />
       <item value="1" text="$30,000" />
       <item value="2" text="$50,000" />
    </root>

查询以将数据提取到Listitem中:

Query to extract the data into a Listitem :

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

这不会产生任何结果.请指教.

This yields no results. Please advice.

预先感谢

BB

推荐答案

您可以将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();

这篇关于将XML数据绑定到Dropdownlist C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆