title: C# Load DataTable from LinQ XDocument
body: <p>I have a complex XML which is loaded into XDocument and I want to turn it into a DataTable please. Here is the actual XML:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;XMLCONTAINER version="1.0" incCount="3"&gt;
  &lt;APPLICATION id="APPLICATION_NODE_1" title="" name="55038170812RDOpayment" application="Excel" appfile="%SAAR%\xmlsFs\Dbxmls.xla" command="openDatabaseDesigner"&gt;
  &lt;MAINDATA id="CONTAINER_NODE_2" title=""/&gt;
  &lt;SKY.FORM id="CONTAINER_NODE_3" title="" table="tblMain" database="\\c\E\DARA\Data\Link\RDOpayments.mdb"&gt;
    &lt;FIELD fieldName="RID"&gt;1234567&lt;/FIELD&gt;
    &lt;FIELD fieldName="DateTime"&gt;05/02/2019 09:00:50&lt;/FIELD&gt;
    &lt;FIELD fieldName="DateOfLetter"&gt;29/01/2019&lt;/FIELD&gt;
    &lt;FIELD fieldName="Name"&gt;Mr Joe Bloggs&lt;/FIELD&gt;
    &lt;FIELD fieldName="NRID"&gt;XXX1234X&lt;/FIELD&gt;
    &lt;FIELD fieldName="paymentAmount"&gt;776.40&lt;/FIELD&gt;
    &lt;FIELD fieldName="Amountfor0809"&gt;776.40&lt;/FIELD&gt;
    &lt;FIELD fieldName="ACE"&gt;FALSE&lt;/FIELD&gt;
    &lt;FIELD fieldName="Telephone"&gt;123456789&lt;/FIELD&gt;
    &lt;FIELD fieldName="AcceptWithheldNumber"&gt;FALSE&lt;/FIELD&gt;
    &lt;FIELD fieldName="PotentialACECase"&gt;FALSE&lt;/FIELD&gt;
    &lt;FIELD childID="CONTAINER_NODE_3_field7option1" fieldName="CustomerType"&gt;3&lt;/FIELD&gt;
    &lt;FIELD fieldName="ContactHistory"&gt;05/02/2019&lt;/FIELD&gt;
    &lt;FIELD fieldName="ACERed"&gt;FALSE&lt;/FIELD&gt;
  &lt;/SKY.FORM&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>The DataTable needs to look like this:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>RID DateTime  DateOfLetter Name  NRID paymentAmount ACE
  --- -----------  ------------ ----   ---- -------------  ----------
  1234567 2019-02-05 10:23:51 2019-02-05 MISS LL TEST X1234X 123.45  FALSE
  7654321 2019-01-11 11:11:11 2019-02-03 MR I WONG Y4321Y 321.21  TRUE</code></pre>
  </div>
  </div>
  </p>
  
  <p>My current attempts of coding this are not picking up the xml as Node? I think it should be attributes xattribute??</p>
  
  <pre><code>// Conversion Xml file to DataTable
      public DataTable CreateDataTableXML(string XmlFile)
      {
          XmlDocument doc = new XmlDocument();
  
          doc.Load(XmlFile);
  
          DataTable Dt = new DataTable();
  
          try
          {
              Dt.TableName = GetTableName(XmlFile);
              XmlNode NodeStructure = doc.DocumentElement.ChildNodes.Cast&lt;XmlNode&gt;().ToList()[0];
              progressBar1.Maximum = NodeStructure.ChildNodes.Count;
              progressBar1.Value = 0;
              foreach (XmlNode column in NodeStructure.ChildNodes)
              {
                  Dt.Columns.Add(column.Name, typeof(String));
                  Progress();
              }
  
              XmlNode Xnodes = doc.DocumentElement;
              progressBar1.Maximum = Xnodes.ChildNodes.Count;
              progressBar1.Value = 0;
              foreach (XmlNode xnode in Xnodes.ChildNodes)
              {
                  List&lt;string&gt; Values = xnode.ChildNodes.Cast&lt;XmlNode&gt;().ToList().Select(x =&gt; x.InnerText).ToList();
                  Dt.Rows.Add(Values.ToArray());
                  Progress();
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.ToString());
          }
          return Dt;
      }
  </code></pre>