Parse XML and populate in List Box

不羁岁月 提交于 2019-12-01 01:45:58

You could use Linq to XML to do it like this.

XDocument xmldoc = XDocument.Load(xmlStream);
var items = (from i in xmldoc.Descendants("item")
             select new { Item = i.Element("SEL").Value, Value = i.Element("VALUE").Value }).ToList();

listBox1.DataSource = items;
listBox1.DisplayMember = "Item";
listBox1.ValueMember = "Value";

Using Linq-to-XML, you can do this:

public partial class item
{
    public object CHK { get; set; }
    public int SEL { get; set; }
    public string VALUE { get; set; }
}

and somewhere in your code:

XDocument lbSrc = XDocument.Load("yourfile.xml");

List<item> _lbList = new List<item>();

foreach (XElement item in lbSrc.Descendants("item"))
{
   _lbList.Add(new item { CHK= item.Element("CHK").Value, 
                          SEL = Convert.ToInt32(item.Element("SEL").Value), 
                          VALUE = item.Element("VALUE").Value });
 }

and then assign that to your listbox:

lbYourListbox.DataSource = _lbList;
lbYourListbox.DisplayMember = "VALUE";
lbYourListbox.ValueMember = "SEL";

That should do it!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!