5

How to populate combobox with values from an xml file.

anasooya
  • 135
  • 1
  • 5
  • 11

3 Answers3

7

Using XmlDocument class you can loop thru the nodes of xml file and then just go on adding items to dropdownlist. Sample code:

 XmlDocument doc = new XmlDocument();
    doc.Load(Server.MapPath("regis.xml"));
    XmlNodeList colorList = doc.SelectNodes("Information/Comments/Name");
    foreach (XmlNode Name in colorList)
    {
      DropDownList1.Items.Add(Name.InnerText);
    }

Ref : http://r4r.co.in/asp.net/01/tutorial/asp.net/How%20to%20populate%20combobox%20from%20xml%20file%20using%20c-Sharp%20in%20asp.net.shtml

pramodtech
  • 6,300
  • 18
  • 72
  • 111
  • will the same code be applicable for windows form application – anasooya May 03 '11 at 05:19
  • not really...I'm not a win form developer but I guess the logic should be more or less the same. Looping thru file and then adding to combobox. Do some google search. – pramodtech May 03 '11 at 05:32
  • ok i got that but my xml file is bit complicated i am not being able to understand how to select the nodes like in the code you have send. – anasooya May 03 '11 at 06:25
  • umm..I think I can't help beyond this...check this one if it helps http://www.dotnettutorials.com/tutorials/xml/winform-filter-xml-cs.aspx – pramodtech May 03 '11 at 08:48
4

You will have to read in the data from the file and you can use something like dataset.ReadXML() and then use that to set your binding for your combobox.

Here is an example to get you started. http://www.codeproject.com/KB/cs/dropdownfromxml.aspx

Update: Note that there are two DataGrid classes. The one that has the DataBind() method is in the System.Web.UI.WebControls namespace. The windows form control does not hav the DataBind method and should work without that line. See: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagrid.datasource.aspx

Beth Lang
  • 1,889
  • 17
  • 38
  • i saw the example of this link but i am getting error in last line datagrid.databind(); and cnt figure out the error – anasooya May 03 '11 at 05:26
  • Can you post the error you're getting using the example from the link? – KaeL May 03 '11 at 05:48
  • 'System.Windows.Forms.DataGrid does not contain a definition for 'DataBind' and no extension method 'DataBind' accepting a first argument of type 'System.Windows.Forms.DataBind' could be found (are you missing a using directive or an assembly reference?) – anasooya May 03 '11 at 06:19
2

Given this XML

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <node1 attribute1="attrib1" attribute2="attrib2">
        <node2>
            <node3>Item1</node3>
            <node3>Item2</node3>
            <node3>Item3</node3>
        </node2>
    </node1>
</root>

We can get to the data a few ways. This class has two methods, the first will loop through all the nodes until it gets to the data we want. The second will use the XmlDocument.GetElementsByTagName() method to go right to the data we want.

using System;
using System.Xml;
using System.Collections.Generic;

public static class MyXmlParser
{
    ///This method will loop through each node to get to the data we want.
    public static List<string> GetItemsFromXmlByLoopingThroughEachNode(string Filename)
    {
        //Create a list to store all the items.
        List<string> Items = new List<string>();

        //Load the document from a file.
        XmlDocument doc = new XmlDocument();
        doc.Load(Filename);

        //Loop through all the nodes in the document.
        foreach(XmlNode RootNode in doc.ChildNodes)
        {
            if(RootNode.NodeType != XmlNodeType.XmlDeclaration)
            {//If the node is not the declaration node parse it.

                //Loop through all the child nodes of <root>
                foreach(XmlNode Node1Node in RootNode.ChildNodes)
                {
                    //Read Attributes of <node1>
                    XmlAttributeCollection attributes = Node1Node.Attributes;
                    XmlAttribute Attribute1 = attributes["attribute1"];
                    //Attribute1.Value will give you the string contained in the attribute.

                    //Loop through all child nodes of <node1>
                    foreach(XmlNode Node2Node in Node1Node.ChildNodes)
                    {
                        //Loop through all child nodes of <node2>
                        foreach(XmlNode Node3Node in Node2Node.ChildNodes)
                        {
                            //These nodes contain the data we want so lets add it to our List.
                            Items.Add(Node3Node.InnerText);
                        }
                    }
                }           
            }
        }
        //Return the List of items we found.
        return Items;
    }

    ///This method will use GetElementsByTagName to go right to the data we want.
    public static List<string> GetItemsFromXmlUsingTagNames(string Filename, string TagName)
    {
        //Create a list to store all the items.
        List<string> Items = new List<string>();

        //Load the document from a file.
        XmlDocument doc = new XmlDocument();
        doc.Load(Filename);

        //Get all the <node3> nodes.
        XmlNodeList Node3Nodes = doc.GetElementsByTagName(TagName);
        //Loop through the node list to get the data we want.
        foreach(XmlNode Node3Node in Node3Nodes)
        {
            //These nodes contain the data we want so lets add it to our List.
            Items.Add(Node3Node.InnerText);
        }
        //Return the List of items we found.
        return Items;       
    }
}

Once you have the data you need, you can add the items to the ComboBox

//Get the items from the XML file.
List<string> Items = MyXmlParser.GetItemsFromXmlUsingTagNames("C:\\test.xml","node3");
//Add them to the ComboBox
ComboBox1.Items.AddRange(Items.ToArray())

See

XmlDocument

XmlNodeList

XmlNode

XmlAttributeCollection

XmlAttribute

Tester101
  • 8,042
  • 13
  • 55
  • 78