Linq to xml in datatable



  • Hello. We need to get the data out of the xml file. It is unclear that it has two levels of investment (not including the root-container itself). Data from there needs to be placed in the DataTable table, but there are two levels, so in two tables? The request for linq didn't cause any problems. Now, how do you put the data in two tables for me? (How to process el.prices)

    Code:

    XDocument doc = XDocument.Load("products_OUT2.xml");
                var query =
                    from product in doc.Root.Elements("PRODUCT")
                    select new
                    {
                        code = (Int32)product.Element("CODE"),
                        name = (string)product.Element("NAME"),
                        vendor = (string)product.Element("VENDOR"),
                        country = (string)product.Element("COUNTRY"),
                        vendorbarcode = (string)product.Element("VENDORBARCODE"),
                        valid_date = (DateTime)product.Element("VALID_DATE"),
                        qtty = (Int32)product.Element("QTTY"),
                        packqtty = (Int32)product.Element("PACKQTTY"),
                        prices =
                        (
                        from price in product.Elements("PRICES")
                        select new
                        {
                            area = (string)price.Element("PRICE").Attribute("area").Value,
                            prc = (double)price.Element("PRICE")
                        }
                        )
                    };
    
            foreach (var el in query)
            {
                dt.Rows.Add(
                    el.code,
                    el.name,
                    el.vendor,
                    el.country,
                    el.vendorbarcode,
                    el.valid_date,
                    el.qtty,
                    el.packqtty,
                    el.prices = ?
                    );
            }
    

    You're the one who said:

    <?xml version="1.0" standalone="yes"?>
    <DocumentElement>
    <PRODUCT>
    <CODE>73</CODE>
    <NAME>Бессмертника цветки 30г ф/чай </NAME>
    <VENDOR>ООО "Алтай-Фарм"</VENDOR>
    <COUNTRY>РОССИЯ</COUNTRY>
    <VENDORBARCODE>4607102610548</VENDORBARCODE>
    <VALID_DATE>2019-01-01T00:00:00+07:00</VALID_DATE>
    <QTTY>358</QTTY>
    <PACKQTTY>1</PACKQTTY>
    <PRICES>
    <PRICE area="1">78.50</PRICE>
    <PRICE area="2">78.90</PRICE>
    <PRICE area="3">82.30</PRICE>
    <PRICE area="4">86.10</PRICE>
    </PRICES>
    </PRODUCT>
    <PRODUCT>
    <CODE>76</CODE>
    <NAME>Гигрометр психрометрический ВИТ-1 (0-25 град.C)</NAME>
    <VENDOR>Стеклоприбор</VENDOR>
    <COUNTRY>УКРАИНА</COUNTRY>
    <VENDORBARCODE>9920007031665</VENDORBARCODE>
    <VALID_DATE>2025-01-01T00:00:00+07:00</VALID_DATE>
    <QTTY>166</QTTY>
    <PACKQTTY>1</PACKQTTY>
    <PRICES>
    <PRICE area="1">78.50</PRICE>
    <PRICE area="2">78.90</PRICE>
    <PRICE area="3">82.30</PRICE>
    <PRICE area="4">86.10</PRICE>
    </PRICES>
    </PRODUCT>
    </DocumentElement>



  • You will first decide how the user will be more comfortable watching and editing these data. You can put it in one table, you can get a few.

    For example, all xml can be loaded DataSet by ReadXml:

    var dataSet = new DataSet();
    dataSet.ReadXml("test.xml");
    

    There will be three tables on the date of the visit: PRODUCT, PRICES (vasible, not to be shown to the user) and PRICE (with area columns and PRICE_text).

    These tables are simpler, tied to them. DataGridView:

    dataGridView.DataSource = dataSet.Tables[0];
    

    If there is no need for any columns, you can hide them; the headings can be renamed:

    dataGridView.DataSource = dataSet.Tables[2];
    dataGridView.Columns["area"].Visible = false;
    dataGridView.Columns["PRICE_text"].HeaderText = "Price";
    

    The problem is, if the user is right in. DataGridView add new lines, if data are retained by the method DataSet.WriteXml There's no way the lines could come out where they want. So there's a new table. This method is therefore only appropriate for the data, without editing.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2