【问题标题】:reading from xml (linq) to windows form从 xml (linq) 读取到 windows 窗体
【发布时间】: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
  • 你为什么到处使用ascoursecode[i]coursename[i]?不要使用ArrayList - 请改用List&lt;T&gt;。 (另外,为System.Drawing 设置一个using 指令,这样您就不需要到处限定它了。)

标签: c# xml forms linq


【解决方案1】:

正如 Mzn 所说,标准文档应该是查看的地方。您将使用的一些方法是ElementsDescendantsCount,其余的获取Value 属性。

请注意,因为您没有在 xml sn-p 中指定如何为 &lt;CourseName&gt;&lt;CourseCode&gt; 存储数据,我认为更糟糕的是,您想要的值没有存储为其中的属性如果Value 属性将从该节点及其子节点返回 all 值。如果它们是属性,则更容易,因为您可以使用Attribute 方法获取数据的string

获取数据后,只需创建文本框、定位它们,然后将数据插入到它们各自的Text 属性中。

提示:使用List&lt;T&gt; 而不是ArrayListList&lt;T&gt; 是类型安全的,因此消除了铸造失败造成的错误。此外,我发现在学习 LINQ 时使用“点表示法”是一个好的开始,因为它遵循与 vanilla c# 代码相同的约定。

【讨论】:

  • so 将课程设置为元素和课程名称和课程代码属性会更好吗? 现在 coursename 和 coursecode 是 course 内的元素
  • 是的。举一个 OP 中的设置示例,如果您要获取名称和代码为您刚刚提供的课程节点的值,则该值将是“asaasas”。
【解决方案2】:

这样的东西会起作用吗?它不使用 linq 查询,但确实将课程放入 List&lt;&gt;

        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();

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多