C# 需要从XML文件填充几个标签,有没有更快的方法?

C# 需要从XML文件填充几个标签,有没有更快的方法?,c#,linq-to-xml,ienumerable,C#,Linq To Xml,Ienumerable,我加载了一个XML文件,将文档分解为Ienumerable,然后将每个元素放入winform上的标签中。到目前为止,我有以下代码,这是有效的 public void PopulateGameBoard() { XDocument gameFiles = XDocument.Parse(Properties.Resources.Jeopardy); IEnumerable<string> categories =

我加载了一个XML文件,将文档分解为Ienumerable,然后将每个元素放入winform上的标签中。到目前为止,我有以下代码,这是有效的

public void PopulateGameBoard()
    {
         XDocument gameFiles = XDocument.Parse(Properties.Resources.Jeopardy);

         IEnumerable<string> categories =
             from category in gameFiles.Descendants("category")
             select (string)category.Attribute("name");


         string first = categories.ElementAt(0);
         cat1HeaderLabel.Text = first;
         string second = categories.ElementAt(1);
         cat2HeaderLabel.Text = second;
         string third = categories.ElementAt(2);
         cat3Label.Text = third;
         string fourth = categories.ElementAt(3);
         cat4Label.Text = fourth;
         string fifth = categories.ElementAt(4);
         cat5Label.Text = fifth;

    }
public void PopulateGameBoard()
{
XDocument gameFiles=XDocument.Parse(Properties.Resources.Jeopardy);
可数范畴=
从gameFiles.substands中的类别(“类别”)
选择(字符串)category.Attribute(“名称”);
string first=categories.ElementAt(0);
cat1HeaderLabel.Text=第一个;
第二个字符串=categories.ElementAt(1);
cat2HeaderLabel.Text=秒;
string third=categories.ElementAt(2);
cat3Label.Text=第三类;
第四个字符串=类别。元素at(3);
cat4Label.Text=第四个;
第五个字符串=类别。元素(4);
cat5Label.Text=第五类;
}
最终产品是Jeopardy Game Board,其中类别和问题将从XML文件中提取


这是我需要处理的5行中的第一行(5个列表分成5行)。我想知道是否有更好的方法来编写代码,我不会用25条语句将一个变量分配给ElementAt(),然后再将该变量分配给25个。

这里我尝试动态创建标签并将值分配给它们,这是一个手工编写的代码,因此它不会编译,也不会进行必要的更改

public void PopulateGameBoard()
{
     XDocument gameFiles = XDocument.Parse(Properties.Resources.Jeopardy);
     IEnumerable<string> categories =
         from category in gameFiles.Descendants("category")
         select (string)category.Attribute("name");
     Label[] cat1HeaderLabel= new Label[100];  
     int i = 0;
       categories.Each(p =>
       {
           cat1HeaderLabel[i] = new Label();
           cat1HeaderLabel[i].Text = p;
           this.Form.Controls.Add(cat1HeaderLabel[i]);
           i++;
       });
}
public void PopulateGameBoard()
{
XDocument gameFiles=XDocument.Parse(Properties.Resources.Jeopardy);
可数范畴=
从gameFiles.substands中的类别(“类别”)
选择(字符串)category.Attribute(“名称”);
标签[]cat1HeaderLabel=新标签[100];
int i=0;
类别。每个(p=>
{
cat1HeaderLabel[i]=新标签();
cat1HeaderLabel[i].Text=p;
this.Form.Controls.Add(cat1HeaderLabel[i]);
i++;
});
}