Walter Fabio Simoni Walter Fabio Simoni - 1 year ago 71
C# Question

Generate automatically DataTable from an XML

Is there any possibility to automatically populate the dataTable from an XML document ?

By automatically, I mean, autogenerate rows/columns.

Example an XML to use :

<?xml version="1.0" encoding="UTF-8"?>
<tns:factures xmlns:xsi="" xmlns:tns="http://XXX" xsi:schemaLocation="xxxxx.xsd">
<facture num="F0000047672" numArchivage="000" dateCompostage="2016-04-14" numCompostage="20160414000333" numTVAIntracom="FR09429449457" nomFournisseur="TT" numCommande="09457519" societeFacturee="005" typeFacture="FAC" montantHT="3817.920" montantTTC="4027.910" montantTVA="209.990" devise="EUR" dateFacture="2016-04-04" dateEcheance="2016-05-31" CodeSiteLivraison="630N" factureDetaillee="false" nomFichier="KKK_005_20160414_000333.pdf">
<ligneTVA tauxTVA="5.500" montantHT="3817.920" montantTVA="209.990" montantTTC="4027.910" />
<facture num="970928" numArchivage="000" dateCompostage="2016-04-14" numCompostage="20160414000365" numTVAIntracom="FR64482283694" nomFournisseur="jfjfjfj" numCommande="02090828" societeFacturee="005" typeFacture="FAC" montantHT="28623.980" montantTTC="34348.780" montantTVA="5724.800" devise="EUR" dateFacture="2016-04-11" dateEcheance="2016-06-10" CodeSiteLivraison="02K" factureDetaillee="false" nomFichier="KKK_005_20160414_000365.pdf">
<ligneTVA tauxTVA="20.000" montantHT="28623.980" montantTVA="5724.800" montantTTC="34348.780" />

As you can see, there is a "facture" node and a "LigneTVA" node.

I would like to add these two nodes automatically in a dataTable without specifying each column.

Is this possible ?

Using Linq-to-SQL? Or something else?

Thanks a lot :)

Answer Source

Try this

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication22
    public partial class Form1 : Form
        const string FILENAME = @"c:\temp\test.xml";
        public Form1()

            DataSet ds = new DataSet();
            dataGridView1.DataSource = ds.Tables[0];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download