8
Reply

Using xml to add controls

Sander Groen

Sander Groen

Jun 9 2010 10:45 AM
4.6k
Hello all,

I like to use xml to add some controls to my application.
The problem is that my app uses only first match in the xml file instead of using all the nodes with the name knop.
I used a foreach loop but stil it does not use all the nodes.

What do i do wrong?

with regards Sander.

public Form1()
        {
            InitializeComponent();
            dialoog = new editaction();

            XmlDocument doc = new XmlDocument();
            doc.Load("test.xml");
            XmlNodeList List = doc.GetElementsByTagName("actions");
            foreach (XmlNode node in List)
            {
                XmlElement knopElement = (XmlElement)node;
                string item = knopElement.GetElementsByTagName("knop")[0].InnerText;
                int positie = 10;
                int tekst = 1;
                create_button(item, positie, tekst);
                positie += 50;
                tekst += 1;
            }
        }

        private void create_button(string xmltext, int positie, int tekst)
        {
            Button btn = new Button();
            btn.Text = xmltext;
            btn.Left = 50;
            btn.Top = positie;
            Label lbl = new Label();
            lbl.Text = tekst.ToString();
            lbl.Top = positie;
            lbl.Left = 10;
            pnlDefault.Controls.Add(btn);
            pnlDefault.Controls.Add(lbl);
        }

Answers (8)