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

问题描述 投票:0回答:1

我在应用程序中使用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列,但它只填充了一行数据,尽管<BOQ Division="Mechanical">有2个项目

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

c# xml datagridview
1个回答
1
投票

试试这个

        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();
© www.soinside.com 2019 - 2024. All rights reserved.