title: How to write data from datagridview to XML
body: <p>I have added a lot of blank lines to the datagridview and I want to write the data to xml. I've done a few little researches, but I'm glad that you did not do it the way I wanted to.</p>
  
  <pre><code>DataTable dt = new DataTable();
  dt.TableName = "SD";
  dt = dataGridView1.DataSource as DataTable;
  dt.WriteXml("SD.xml");
  </code></pre>
  
  <p>it's not working or i couldn't run it.</p>
  
  <p>Its error message:</p>
  
  <blockquote>
    <p>System.NullReferenceException: 'Object reference not set to an
    instance of an object.'</p>
  </blockquote>