C# 从xml(linq)读取到windows窗体

C# 从xml(linq)读取到windows窗体,c#,xml,forms,linq,C#,Xml,Forms,Linq,我使用linq创建了一个XML文件。(用户填写一些文本框,提交时,数据将以以下格式添加到XML中: <Departments> <Department1> <Course1 code="x"> <CourseName></CourseName> <CourseCode></CourseCode> </Course1>

我使用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>
但是我不知道如何进行linq查询,就像我上面所说的,将其值分配给textboxt或标签


更新:管理显示它。感谢您的帮助。

正如Mzn所说,标准文档应该是您可以查看的地方。您将使用的一些方法包括
元素
子体
计数
,以及为其余部分获取
属性

需要注意的是,由于您没有在xml代码片段中指定如何存储
的数据,我假设更糟糕的情况是,您想要的值没有存储为属性,在这种情况下
Value
属性将返回该节点及其子节点的所有值。如果它们是属性,则会返回由于可以使用
属性
方法获取数据的
字符串
,因此更加容易

一旦你把数据拿出来,就只需要创建文本框,定位它们,然后将数据插入它们各自的
Text
属性中


提示:使用
列表
而不是
数组列表
列表
是类型安全的,因此可以消除失败的强制转换错误。此外,我发现使用“点表示法”对于LINQ来说,学习它是一个很好的开始,因为它遵循与普通c#code相同的约定。

是否需要这样的工作?它不使用LINQ查询,但它确实将课程放入
列表中

XDocument doc=XDocument.Load(“YourXmlFileNameHere.xml”);
列出课程第1部分;
//第一步
CoursesInDepartment1=doc.Element(“Departments”).Element(“Department1”).Elements().ToList();
//步骤2
foreach(课程中的元素第1部分){
TextBox nameBox=新建TextBox();
TextBox codeBox=新的TextBox();
//设置位置、大小等。
//步骤3
nameBox.Text=elem.Element(“CourseName”).Value;
codeBox.Text=elem.Element(“CourseCode”).Value;
this.Controls.Add(nameBox.Text);
this.Controls.Add(codeBox.Text);
}
Console.ReadLine();

希望这有帮助!

常规文档可以帮助您做到这一点……如果您需要其他选择,可以参考我的博客:为什么您要将
as
coursecode[i]
coursename[i]
一起使用?不要使用
ArrayList
-而是使用
List
。(另外,为
系统绘图
提供
使用
指令,这样您就不必到处对其进行限定。)因此,最好将课程设置为元素,并设置coursename和coursecode属性?现在coursename和coursecode是CourseEyes中的元素。为了给您一个在OP中设置的示例,如果您要获得课程节点的值,其名称和代码与您刚才提供的相同,则该值应为“asasas”。
          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);
         }
        XDocument doc = XDocument.Load("YourXmlFileNameHere.xml");
        List<XElement> CoursesInDepartment1;

        // Step 1
        CoursesInDepartment1 = doc.Element("Departments").Element("Department1").Elements().ToList();

        // Step 2
        foreach (XElement elem in CoursesInDepartment1) {
            TextBox nameBox = new TextBox();
            TextBox codeBox = new TextBox();
            //Set the location, size, ect.

            //Step 3
            nameBox.Text = elem.Element("CourseName").Value;
            codeBox.Text = elem.Element("CourseCode").Value;

            this.Controls.Add(nameBox.Text);
            this.Controls.Add(codeBox.Text);
        }

        Console.ReadLine();