IEnumerable<XElement> 多次获取相同的元素

IEnumerable<XElement> is fetching same element more than once

我正在尝试使用 C# 中的 XDocumentXelement 获取 xml 数据。我正在使用 IEnumerable<XElement> 遍历我想要获取的所有元素。我的问题是,IEnumerable<XElement> 多次获取相同的数据,当我使用 foreach 循环时,它循环的次数超过了我的 xml 文件中的元素数。它在每个循环中获取相同的东西。有什么建议吗?

我的XML文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SYSTEM SelectedConfiguration = "5840-0000-0001">
  <Configuration Name = "5840-0000-0001">
     <Users>
        <User>
          <Name>Lebron</Name>
          <Surname>James</Surname>
          <Adress>asdfgh</Adress>
          <Phone>1234</Phone>
          <Gender>Male</Gender>
          <Country>USA</Country>
        </User>
        <User>
          <Name>Kevin</Name>
          <Surname>Durand</Surname>
          <Adress>asdfghasdfgh</Adress>
          <Phone>4567</Phone>
          <Gender>Male</Gender>
          <Country>USA</Country>
        </User>
        <User>
          <Name>Stephen</Name>
          <Surname>Curry</Surname>
          <Adress>zxcv</Adress>
          <Phone>1267</Phone>
          <Gender>Male</Gender>
          <Country>USA</Country>
        </User>
    </Users>
  </Configuration>  
</SYSTEM>   

我的代码:

 XDocument devices = XDocument.Load(Application.StartupPath + "\Users.xml");
                    XElement conf = devices.Root.Element("Configuration");

                  /*
                    IEnumerable<XElement> childElements =
                        from el in conf.Element("Instruments").Elements("Instrument")
                        select el;
                   */
                    IEnumerable<XElement> childElements = conf.Element("Users").Elements("User");
                    int counter = childElements.Count();
                    foreach (XElement el in childElements)
                    {


                            DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
                            row.Cells[0].Value = el.Element("Name").Value.ToString();
                            row.Cells[1].Value = el.Element("Surname").Value.ToString();
                            row.Cells[2].Value = el.Element("Adress").Value.ToString();

                            dataGridView1.Rows.Add(row);
                           // if (counter-- <= 1)
                               // return;
                    }

我得到的结果:

Click here to see the image

Click here to see the image 2

Click here to see the image 3

如图所示,有不止一个 Lebron James 或 Kevin Durand,并且 foreach 循环遍历这些节点的每个组合,如图 3 所示。

编辑 1:

程序多次循环回到 IEnumerable<XElement> childElements = conf.Element("Users").Elements("User"); 行。计数器一次又一次地得到 3。

这对我有用,请看一下。

XDocument devices = XDocument.Load(Application.StartupPath + "\Users.xml");
XElement conf = devices.Root.Element("Configuration");

List < string > list = new List < string > ();
IEnumerable < XElement > childElements = conf.Element("Users").Elements("User");
var dataList = childElements.Select(el => new {
 Name = el.Element("Name").Value.ToString(),
  Surname = el.Element("Surname").Value.ToString(),
  Adress = el.Element("Adress").Value.ToString()
});

foreach(var userInfo in dataList) {
 list.Add($ "{userInfo.Name}:{userInfo.Surname}:{userInfo.Adress}");
}