0

Here is some code I have already have which outputs the data :

{ SubjectName = maths, SubjectId = qq1, SubjectValue = 20 }
{ SubjectName = science, SubjectId = sla1s, SubjectValue = 25 }

here is the code which does this:

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

foreach (var subject in subjects)
{
   Console.WriteLine(subject);

   string subjectName = subject.SubjectName;
   string subjectId = subject.SubjectId;
   string subjectValue = subject.SubjectValue;

textBox1.Text = "Subject Name :" + moduleName + 
                "Subject Id :" + moduleCode + 
                "Subject Value :" + moduleCredit;
}

My problem is when i try to add the string variable to textBox1 using the code :

textBox1.Text = "Subject Name :" + moduleName + "Subject 
                      Id :" + moduleCode + "Subject Value :" + moduleCredit;

only the second data is displayed which is { SubjectName = science, SubjectId = sla1s, SubjectValue = 25 }.

How can I make it so that I can store both output in different textfields so that both are displayed.

{ SubjectName = maths, SubjectId = qq1, SubjectValue = 20 }
{ SubjectName = science, SubjectId = sla1s, SubjectValue = 25 }
Jimi
  • 29,621
  • 8
  • 43
  • 61
Steven
  • 79
  • 2
  • 11
  • 3
    Use `textBox1.AppendText();` and also append a line feed at the end of your string. This textbox must be multiline of course. – Jimi Dec 09 '17 at 22:31

2 Answers2

0

You could use textBox1.Text += "YourText"

Michael
  • 120
  • 9
0

As someone suggested in your question comments, use AppendText. As stated in the MSDN:

The AppendText method enables the user to append text to the contents of a text control without using text concatenation, which, can yield better performance when many concatenations are required.

This means that concatenation like this:

textBox1.Text += "something";

will be less performant than:

textBox1.AppendText("something");

but only if the Multiline property of the TextBox is set to true (with Multiline = false the concatenation will be faster instead).

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • I have used the append method and now its storing both data of the 2 subjects however how can i make it so that a new label is created instead of specificing a static textBox1. so something like textbox[i] = new textbox(); textbox[i].AppendText("something"); since it will use a for loop to do this where would it be placed within the code. Sry for asking many questions, im quite new to programming using xml files so am abit unsure at parts. – Steven Dec 09 '17 at 23:05