Parse XML and populate in List Box

前端 未结 2 1450
星月不相逢
星月不相逢 2021-01-07 13:03

I\'m a newbie to C#.

I want to develop C# List box in Windows Form. I found this link to be helpful. But the input to the List box will be an XML of th

2条回答
  •  温柔的废话
    2021-01-07 13:47

    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 _lbList = new List();
    
    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!

提交回复
热议问题