Search code examples
c#xmlwinformsdynamicxelement

How can i iterate through if statement with condition if (i < current Subject nodes inside xml file ) then display data for each


Here is my current code which outputs the modules in 1 single input textbox.

XElement root = XElement.Load("Data.xml");
            var subjects = from subject in root.Descendants()
                          where subject.Name.LocalName.Contains("Subject")
                          select new
                          {
                              subname = subject.Element("subjectName").Value,
                              subid = subject.Element("subjectId").Value,
                              subvalue = subject.Element("subjectvalue").Value
                          };

            foreach (var subject in subjects)
            {
                Console.WriteLine(subject); 
                //you can use subject like this:
                string subName = subject.subname;
                string subID = subject.subid;
                string subValue = subject.subvalue;
                TextBox[] info = new TextBox[6];



                textBox1.AppendText("Subject Name :  " + subName + "  Subject Id :  " + subID + "  Subject Value :  " + subValue + " " );
            }

How can i alter this code so that i dynamically create a inputtextbox depending on how many tags currently existing inside my xml file.

here is some code i tried :

 foreach (var subject in subjects)
            {
                Console.WriteLine(module);
                subname = subject.Element("subjectName").Value,
                subid = subject.Element("subjectId").Value,
                subvalue = subject.Element("subjectvalue").Value

            for (int i = 0; i < 2; i++)
            {
                info[i] = new TextBox();
                info[i].Location = new System.Drawing.Point(50, 114 + i * 25);
                info[i].Size = new System.Drawing.Size(300, 15);
                info[i].Text = @"";
                this.Controls.Add(info[i]);

                info[i].AppendText("Subject Name :  " + subName + "  Subject 
                Id :  " + subID + "  Subject Value :  " + subValue + " " );
            }
        }

however this code displays the same data on both the dynamically created inputbox field. How can I make it so that the if condition is something like if (i < current Subject nodes inside xml file ) then iterate through them displaying each data into a dymaically created input textbox.

here is how my xml file looks like :

<PersonDetails>
  <Person>Teacher</Person>
  <keystage3>
    <Subject>
      <subjectName>maths</subjectName>
      <subjectId>qq1</subjectId>
      <subjectvalue>20</subjectvalue>
    </Subject>
    <Subject>
      <subjectName>english</subjectName>
      <subjectId>aaa</subjectId>
      <subjectvalue>40</subjectvalue>
    </Subject>
  </keystage3>
  <keystage4>
  </keystage4>
</PersonDetails>

Solution

  • As per above explanation, try this:

        int y = 114;
        foreach (var subject in subjects)
        {
                Console.WriteLine(module);
                subname = subject.Element("subjectName").Value,
                subid = subject.Element("subjectId").Value,
                subvalue = subject.Element("subjectvalue").Value
    
                TextBox box = new TextBox();
                box.Location = new System.Drawing.Point(50, y);
                box.Size = new System.Drawing.Size(300, 15);
                box.Text = @"Subject Name :  " + subName + "  Subject Id :  " + subID + "  Subject Value :  " + subValue + " ";
                this.Controls.Add(box);
                y += 25;
        }