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);
}
Loading

PJ MartinsRank #2,548 · 548 reputationPosted Jun 9, 2010, 10:22 AM EDT
Sander Groen12 reputationPosted Jun 9, 2010, 10:38 AM EDT
PJ MartinsRank #2,548 · 548 reputationPosted Jun 9, 2010, 10:55 AM EDT
Kapil Deo MalhotraRank #2,120 · 672 reputationPosted Jun 9, 2010, 11:11 AM EDT
You are getting the element name by "actions" where actions is root node. Instead you should use action
i.e.
XmlNodeList List = doc.GetElementsByTagName("action");
Hope that will help you
Sander Groen12 reputationPosted Jun 9, 2010, 11:27 AM EDT
I still only get the first button knop1.
Kapil Deo MalhotraRank #2,120 · 672 reputationPosted Jun 9, 2010, 11:50 AM EDT
I debug the application and i get the list for knob and knob1. Have you tried to debug and see you are getting both the child, may be some issue with create_button function which is not displaying the button correctly.
Kapil Deo MalhotraRank #2,120 · 672 reputationPosted Jun 9, 2010, 11:51 AM EDT
public Form1()
{
InitializeComponent();
XmlDocument doc = new XmlDocument();
String xml = "" +
"
"
"
"
"
"
"
doc.LoadXml(xml);
XmlNodeList List = doc.GetElementsByTagName("action");
int increment = 10;
int positie = 10;
int tekst = 0;
foreach (XmlNode node in List)
{
XmlElement knopElement = (XmlElement)node;
string item = knopElement.GetElementsByTagName("knop")[0].InnerText;
positie = positie + increment;
tekst += 1;
create_button(item, positie, tekst);
}
}
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);
}
}
Sander Groen12 reputationPosted Jun 9, 2010, 12:08 PM EDT
Thank you all!