title: DataTable to XML Using Loop
body: <p>I want to Convert My Data Table to XML. I Have two data Table Similar like this.</p>
  
  <p><img src="https://i.stack.imgur.com/WdBQq.png" alt="enter image description here"></p>
  
  <p>I want to convert similar like this.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;root&gt;
    &lt;member&gt;
      &lt;refid&gt;1&lt;/refid&gt;
      &lt;fname&gt;Indocin&lt;/fname&gt;
      &lt;lname&gt;David&lt;/lname&gt;
      &lt;activities&gt;
        &lt;refid&gt;1&lt;/refid&gt;
        &lt;act&gt;Swimming&lt;/act&gt;
      &lt;/activities&gt;
    &lt;activities&gt;
        &lt;refid&gt;1&lt;/refid&gt;
        &lt;act&gt;running&lt;/act&gt;
      &lt;/activities&gt;
      &lt;Date&gt;2013-09-17T18:10:01.3452408+05:30&lt;/Date&gt;
    &lt;/member&gt;
    &lt;member&gt;
      &lt;refid&gt;2&lt;/refid&gt;
      &lt;fname&gt;Indocin&lt;/fname&gt;
      &lt;lname&gt;David&lt;/lname&gt;
      &lt;activities&gt;
        &lt;refid&gt;2&lt;/refid&gt;
        &lt;act&gt;playing&lt;/act&gt;
      &lt;/activities&gt;
      &lt;activities&gt;
        &lt;refid&gt;2&lt;/refid&gt;
        &lt;act&gt;running&lt;/act&gt;
      &lt;/activities&gt;
     &lt;activities&gt;
        &lt;refid&gt;2&lt;/refid&gt;
        &lt;act&gt;swimming&lt;/act&gt;
      &lt;/activities&gt;
      &lt;Date&gt;2013-09-17T18:10:01.3452408+05:30&lt;/Date&gt;
    &lt;/member&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>I Have tried Below, but I need to do this with looping with datatable select statement for  refid. can anyone please help me on this.</p>
  
  <pre><code> XDocument doc = new XDocument(new XElement("root",
                                               new XElement("member",
                                                   new XElement("refid", 1),
                                                   new XElement("fname", "Indocin"),
                                                   new XElement("lname", "David"),
                                                   new XElement("activities", new XElement("refid",1),
                                                       new XElement("act","Swimming")),
                                                   new XElement("Date", DateTime.Now))));
  </code></pre>