C# 使用C中的XDocument对DataGridView进行XML查询#

C# 使用C中的XDocument对DataGridView进行XML查询#,c#,xml,datagridview,C#,Xml,Datagridview,我在应用程序中使用XML的知识非常基础,我也为此搜索了很多帖子,但我找不到我要找的内容 我正在尝试制作一个应用程序,其中向用户提供一个XML文件,将其放在某个文件夹中,应用程序读取其数据并将其绑定到DataGridView(DGV) 我的XML文件如下所示: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Project> <PCode>18-01</PCode> <

我在应用程序中使用XML的知识非常基础,我也为此搜索了很多帖子,但我找不到我要找的内容

我正在尝试制作一个应用程序,其中向用户提供一个XML文件,将其放在某个文件夹中,应用程序读取其数据并将其绑定到DataGridView(DGV)

我的XML文件如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Project>

  <PCode>18-01</PCode>
  <PName>Project A</PName>

  <AllBOQ>

      <BOQ Division="Mechanical">
        <Items>
          <Item>
            <Code>M-FF-01</Code>
            <Description>Supply and Install of Seamless Black Steel Pipes, Sch. 40, 1"</Description>
            <Quantity>50</Quantity>
            <Unit>mt</Unit>
          </Item>
          <Item>
            <Code>M-FF-02</Code>
            <Description>Supply and Install of Seamless Black Steel Pipes, Sch. 40, 2"</Description>
            <Quantity>60</Quantity>
            <Unit>mt</Unit>
          </Item>
        </Items>
      </BOQ>

      <BOQ Division="Electrical">
          <Items>
              <Item>
                <Code>E-FA-01</Code>
                <Description>Supply and Install of Fire Alarm</Description>
                <Quantity>15</Quantity>
                <Unit>nr</Unit>
              </Item>
        </Items>
      </BOQ>

  </AllBOQ>

</Project>
应用程序应按以下顺序在DGV中创建4列 “代码”、“说明”、“数量”、“单位” 然后使用此查询用数据填充它们

    private void lbxDivision_SelectedIndexChanged(object sender, EventArgs e)
    {

        // Get selected project XML file
        string selCode = lbxProjects.SelectedValue.ToString();
        lblTest.Text = lbxDivision.Text;
        var boqItems = from itm in selProject.Descendants("BOQ")
                       where itm.Attribute("Division").Value == lbxDivision.Text
                       select new BOQItem()
                       {
                           Code        = itm.Elements("Items").Elements("Item").Elements("Code")       .FirstOrDefault().Value,
                           Description = itm.Elements("Items").Elements("Item").Elements("Description").FirstOrDefault().Value,
                           Quantity    = itm.Elements("Items").Elements("Item").Elements("Quantity")   .FirstOrDefault().Value,
                           Unit        = itm.Elements("Items").Elements("Item").Elements("Unit")       .FirstOrDefault().Value
                       };
        dataGridView1.DataSource = boqItems.ToList();

    }
DGV正在创建4列,但它只填充了一行数据,尽管
有2个项目

如何查询(代码、说明、数量、单位)下具有BOQ Division Attribute=“Mechanical”(或根据用户选择)的所有项目

        var items = selProject.Descendants("BOQ")
                              .Where(boq => boq.Attribute("Division").Value == lbxDivision.Text)
                              .Descendants("Item")
                              .Select(itm => new BOQItem()
                              {
                                   Code        = itm.Elements("Code")       .FirstOrDefault().Value,
                                   Description = itm.Elements("Description").FirstOrDefault().Value,
                                   Quantity    = itm.Elements("Quantity")   .FirstOrDefault().Value,
                                   Unit        = itm.Elements("Unit")       .FirstOrDefault().Value
                              }).ToList();

什么就这样。谢谢想解释一下你的密码吗?对不起;)。您的选择只是每个分区执行一次,而不是每个项目。这就是你只收到一件而不是两件的原因。在上面的代码中,您看到了子体(“项”),因此您得到了分区的每个项。我明白了,这就是为什么我们需要2个lambda表达式“boq”和“itm”,请注意,如果某些节点不存在,查询将抛出异常