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
Sander GroenPosted Jun 9, 2010, 4:08 PM
Thank you all!
Kapil Deo MalhotraPosted Jun 9, 2010, 3:51 PM
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);
}
}
Kapil Deo MalhotraPosted Jun 9, 2010, 3:50 PM
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.
Sander GroenPosted Jun 9, 2010, 3:27 PM
I still only get the first button knop1.
Kapil Deo MalhotraPosted Jun 9, 2010, 3:11 PM
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
PJ MartinsPosted Jun 9, 2010, 2:55 PM
Sander GroenPosted Jun 9, 2010, 2:38 PM
PJ MartinsPosted Jun 9, 2010, 2:22 PM