title: How to Export Full SQL Table to XML
body: <p>My primary coding ideal is on .net applications.. So I have limited skill with application design.</p>
  
  <p>I am trying to export an entire table from a database (Using a view) to an XML file (To then hopefully export that to a spreadsheet - I've looked and fail to find a direct way).</p>
  
  <p>I have successfully exported only 1 column to the xml file, using this code:</p>
  
  <pre><code>DataConn.UserName = "***";
  DataConn.Password = "***";
  DataConn.SqlServer = "***";
  DataConn.Database = "***";
  
  const string strSql = "SELECT TOP 1 * FROM vwGetStaffDetails FOR XML AUTO";
  
  using (SqlCommand sqlComm = new SqlCommand(strSql, DataConn.Connect()) { CommandType = CommandType.Text })
  {
       string result = sqlComm.ExecuteScalar().ToString();
  
       File.WriteAllText(@"C:\Temp\text.xml", result);
   }
  </code></pre>
  
  <p>Whenever I use <code>ExecuteReader</code> or <code>ExecuteXmlReader</code>, I don't get any actual results.</p>
  
  <p>How would I get all the fields?</p>
  
  <p><strong>Edit:</strong> I had to use <code>Top 1</code> to get the <code>Execute Scalar</code> working well.</p>
  
  <p>Using the below solution, my file shows:</p>
  
  <pre><code>&lt;?xml version="1.0" standalone="yes"?&gt;
  &lt;NewDataSet&gt;
    &lt;Table&gt;
      &lt;XML_F52E2B61-18A1-11d1-B105-00805F49916B&gt;&amp;lt;vwGetStaffDetails ImageLoc="****.jpg" LName="GAINES" FName="****" StaffTitle="****" JobPosition="****" Email="***@***.com" Code="***" Number="******" PhoneTypeID="1"/&amp;gt;&lt;/XML_F52E2B61-18A1-11d1-B105-00805F49916B&gt;
    &lt;/Table&gt;
  &lt;/NewDataSet&gt;
  </code></pre>
  
  <p>It's writing <code>&amp;lt;</code> etc instead of proper XML. Is the any way to fix it?</p>