Hello. It is necessary to pull out the data from the xml-file. The incomprehensibility is that it has 2 levels of nesting (not counting the root container itself). The data from there must be placed in the DataTable table, but there are 2 levels here, so in 2 tables? The linq query itself did not cause any problems. But now how to put the data in 2 tables for me a question .. (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 = ? ); } Xml itself:
<?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>