title: Write formatted XML output from stored procedure to file
body: <p>Posting both question and (one) answer here to see if somebody has a better solution...</p>
  
  <p>I am trying to write formatted XML output from stored procedure to file. My first attempt looked like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>// the actual command is a stored procedure that returns an XML document, but use this for demo purposes
  var xmlCommand = new SqlCommand("SELECT 1 as ID, 'asdf' as Name FOR XML PATH, ROOT('MyData')", conn);
  File.WriteAllText("file1.xml", (string)xmlCommand.ExecuteScalar());
  </code></pre>
  
  <p>Which was nice and short, but the file generated has everything stuffed on one line:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;MyData&gt;&lt;row&gt;&lt;ID&gt;1&lt;/ID&gt;&lt;Name&gt;asdf&lt;/Name&gt;&lt;/row&gt;&lt;/MyData&gt;
  </code></pre>
  
  <p>I want it to be formatted nicely:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;MyData&gt;
    &lt;row&gt;
      &lt;ID&gt;1&lt;/ID&gt;
      &lt;Name&gt;asdf&lt;/Name&gt;
    &lt;/row&gt;
  &lt;/MyData&gt;
  </code></pre>