我如何使用条件迭代 if 语句 if (i < current Subject nodes inside xml file) 然后显示每个数据
How can i iterate through if statement with condition if (i < current Subject nodes inside xml file ) then display data for each
这是我当前的代码,它在 1 个单输入文本框中输出模块。
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 + " " );
}
我如何更改此代码,以便根据 xml 文件中当前存在的标签数量动态创建输入文本框。
这是我试过的一些代码:
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 + " " );
}
}
但是此代码在两个动态创建的输入框字段上显示相同的数据。我怎样才能使 if 条件类似于 if (i < current Subject nodes inside xml file) 然后遍历它们,将每个数据显示到动态创建的输入文本框中。
这是我的 xml 文件的样子:
<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>
按照上面的解释,试试这个:
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;
}
这是我当前的代码,它在 1 个单输入文本框中输出模块。
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 + " " );
}
我如何更改此代码,以便根据 xml 文件中当前存在的标签数量动态创建输入文本框。
这是我试过的一些代码:
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 + " " );
}
}
但是此代码在两个动态创建的输入框字段上显示相同的数据。我怎样才能使 if 条件类似于 if (i < current Subject nodes inside xml file) 然后遍历它们,将每个数据显示到动态创建的输入文本框中。
这是我的 xml 文件的样子:
<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>
按照上面的解释,试试这个:
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;
}