0

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>
Steven
  • 79
  • 2
  • 11
  • Why do you need to create two `TextBox`es for each subject? You don't need the `info` array either. What do *you* need is something to increase the y position for each subject so the textboxes don't all sit on top of each other. So declare and initialize `y` before the loop and make sure you increment it appropriately with each iteration. – Dylan Nicholson Dec 10 '17 at 10:38
  • could it be possible if you gave me an example of how this can be written in a different context prehaps so i can understand it more. – Steven Dec 10 '17 at 10:45
  • i made it so that the location of each textbox is different where i change the y positioning with the code 114 + i * 25 . Here is a image of the result i get when the code is run if it helps. https://gyazo.com/206818dd131aaa2b4409abf199c682eb As you may see the fields only display the data from the first Subject node. Code updated showing my xml file. – Steven Dec 10 '17 at 10:54
  • No you didn't, you're using the same two y locations for two textboxes with exactly the same content for every subject. I don't understand why you have that `for (int i; i < 2; i++)` loop at all. You need to increment the y position *for each subject*. – Dylan Nicholson Dec 10 '17 at 10:57
  • It the textbox set to multiline? – jdweng Dec 10 '17 at 10:57

1 Answers1

1

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;
    }
Dylan Nicholson
  • 1,301
  • 9
  • 23
  • Ah so your changing the y positioning everytime. Thanks alot for helping me with this . I really appreaciate it. – Steven Dec 10 '17 at 11:13