开发者

add node names from an xml file into a combobox using c#

Im using c#.net windows form applpication. I have an xml file which contains many nodes. How can I get the names of those nodes into a 开发者_开发技巧combobox. If possible avoid duplicate names.


If you're using .NET 3.5 you can use LINQ to XML to select your nodes.

Alternatively, or if you're not using .NET 3.5, you can use System.Xml.XPath to select your nodes.

After selecting your nodes you can use a foreach on them and insert them one by one in that loop. Alternatively if you have them stored in a List<> you can use ForEach for cleaner code.


You can do this using LINQ to XML:

combobox.DataSource = XDocument.Load(path)
        .Descendants
        .Select(n => n.Name.LocalName)
        .Distinct()
        .ToArray();


This should suit your needs without using LINQ etc:

        foreach (XmlNode node in my_XML_Doc)
        {
            if (!ComboBox1.Items.Contains(node.Name))
            {
                ComboBox1.Items.Add(node.Name);
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜