【发布时间】:2014-05-18 19:40:24
【问题描述】:
我使用 linq 制作了一个 XML 文件。(用户填写一些文本框并在提交时以这种格式将数据添加到 xml 中:
<Departments>
<Department1>
<Course1 code="x">
<CourseName></CourseName>
<CourseCode></CourseCode>
</Course1>
<Course2 code="y">
<CourseName></CourseName>
<CourseCode></CourseCode>
</Course2>
</Department1>
<Department2>
<Course3 code="a">
<CourseName></CourseName>
<CourseCode></CourseCode>
</Course3>
</Department2>
</Departments>
现在我想将 xml 加载到另一个表单并显示我在 Xml 中的数据。 如果我在加载表单时在第一部门有 1 门课程 2 文本框应该出现(一个带有代码,一个带有名称),如果我在加载表单时在部门 1 有 2 门课程,那么它们下面将有 2 个文本框和 2 个其他文本框第二门课程(名称和代码)。 但我不确定,因为我直到最近才使用 c# 和 xml/ling。
我认为的步骤是:
- 了解我在部门 1 有多少课程
- 为我找到多少课程制作文本框
- 运行查询以将课程代码和课程名称放入文本框中。
对于我想到使用 ArrayList 的表单标签/文本框
for( int i = 0 ; i = how many courses I have in Departments1 in the xml file)
{
coursecode.Add(new TextBox());
course.Add(new TextBox());
System.Drawing.Point p = new System.Drawing.Point(150, 100 + i * 30);
(coursecode[i] as TextBox).Location = p;
(course[i] as TextBox).Location = p;
(course[i] as TextBox).Size = new System.Drawing.Size(70, 20);
(course[i] as TextBox).Size = new System.Drawing.Size(70, 20);
this.Controls.Add(coursecode[i] as TextBox);
this.Controls.Add(coursename[i] as TextBox);
}
但我不知道如何进行 linq 查询,就像我上面所说的,将其值分配给文本框或标签。
更新:设法显示它。感谢您的帮助。
【问题讨论】:
-
常规文档可以帮助您解决这个问题...如果您需要替代方案,可以参考我的博客:mazdev.blogspot.ae/2012/04/linq-to-xml.html
-
你为什么到处使用
as和coursecode[i]和coursename[i]?不要使用ArrayList- 请改用List<T>。 (另外,为System.Drawing设置一个using指令,这样您就不需要到处限定它了。)