title: How to return XML in ASP.NET?
body: <p>I have encountered many half-solutions to the task of returning XML in ASP.NET. I don't want to blindly copy &amp; paste some code that happens to work most of the time, though; I want the <em>right</em> code, and I want to know <em>why</em> it's right. I want criticism; I want information; I want knowledge; I want understanding.</p>
  
  <p>Below are code fragments, in order of increasing complexity, representing some of the partial solutions I've seen, including some of the further questions each one causes, and which I'd like to have answered here.</p>
  
  <p>A thorough answer must address why we <em>must</em> have or <em>must not</em> have any of the following things, or else explain why it's irrelevant.</p>
  
  <ul>
  <li>Response.Clear();</li>
  <li>Response.ContentType = "text/xml";</li>
  <li>Response.ContentEncoding = Encoding.UTF8;</li>
  <li>Response.ContentEncoding = Encoding.UTF16;</li>
  <li>Response.ContentType = "text/xml; charset=utf-8";</li>
  <li>Response.ContentType = "text/xml; charset=utf-16";</li>
  <li>Response.End()</li>
  <li>Using an aspx with the front-file guts ripped out</li>
  <li>Using an ashx file</li>
  </ul>
  
  <p>In the end, imagine you need to write the contents of a helper function like this:</p>
  
  <pre class="lang-c# prettyprint-override"><code>///&lt;summary&gt;Use this call inside your (Page_Xxx) method to write the
  ///xml to the web client. &lt;/summary&gt;
  ///&lt;remarks&gt;See for https://stackoverflow.com/questions/543319/how-to-return-xml-in-asp-net
  ///for proper usage.&lt;/remarks&gt;
  public static void ReturnXmlDocumentToWebClient(
      XmlDocument document,
      Page page)
  {
     ...
  }
  </code></pre>
  
  <hr>
  
  <p>Every solution I see starts with taking an empty aspx page, and trimming all the HTML out of the front file (which causes warnings in Visual Studio):</p>
  
  <pre><code>&lt;%@ Page Language="C#"
        AutoEventWireup="true"
        CodeFile="GetTheXml.aspx.cs"
        Inherits="GetTheXml" %&gt;
  </code></pre>
  
  <p>Next we use the <code>Page_Load</code> event to write to the output:</p>
  
  <pre class="lang-c# prettyprint-override"><code>protected void Page_Load(object sender, EventArgs e)
  {
     String xml = "&lt;foo&gt;Hello, world!&lt;/foo&gt;";
  
     Response.Write(xml);
  }
  </code></pre>
  
  <hr>
  
  <p>Do we need to change the <strong>ContentType</strong> to <strong>"text/xml"</strong>? I.e.:</p>
  
  <pre class="lang-c# prettyprint-override"><code>protected void Page_Load(object sender, EventArgs e)
  {
     String xml = "&lt;foo&gt;Hello, world!&lt;/foo&gt;";
  
     Response.ContentType = "text/xml";
     Response.Write(xml);
  }
  </code></pre>
  
  <hr>
  
  <p>Do we need to call <code>Response.Clear</code> first?</p>
  
  <pre class="lang-c# prettyprint-override"><code>protected void Page_Load(object sender, EventArgs e)
  {
     String xml = "&lt;foo&gt;Hello, world!&lt;/foo&gt;";
  
     Response.Clear();
     Response.ContentType = "text/xml";
     Response.Write(xml);
  }
  </code></pre>
  
  <p>Do we really need to call that? Doesn't <code>Response.Clear</code> make the prior step of making sure that the code in the front file was empty (not even a space or a carriage return) outside of the <code>&lt;% ... %&gt;</code> unnecessary?</p>
  
  <p>Does <code>Response.Clear</code> make it more robust, in case someone left a blank line or space in the code-front file?</p>
  
  <p>Is using ashx the same as a blank aspx main file, because it's understood that it's not going to output HTML?</p>
  
  <hr>
  
  <p>Do we need to call <code>Response.End</code>? I.e.:</p>
  
  <pre class="lang-c# prettyprint-override"><code>protected void Page_Load(object sender, EventArgs e)
  {
     String xml = "&lt;foo&gt;Hello, world!&lt;/foo&gt;";
  
     Response.Clear();
     Response.ContentType = "text/xml";
     Response.Write(xml);
     Response.End();
  }
  </code></pre>
  
  <p>What else could possibly happen after <code>Response.Write</code> that needs us to end the response <strong>right now</strong>?</p>
  
  <hr>
  
  <p>Is the content-type of <code>text/xml</code> sufficient, or should it instead be <strong>text/xml; charset=utf-8</strong>?</p>
  
  <pre class="lang-c# prettyprint-override"><code>protected void Page_Load(object sender, EventArgs e)
  {
     String xml = "&lt;foo&gt;Hello, world!&lt;/foo&gt;";
  
     Response.Clear();
     Response.ContentType = "text/xml; charset=utf-8";
     Response.Write(xml);
     Response.End();
  }
  </code></pre>
  
  <p>Or should it specifically <strong>not</strong> be that? Does having a charset in the content type, but not setting the property, screw up the server?</p>
  
  <p>Why not some other content type, e.g.:</p>
  
  <ul>
  <li>UTF-8</li>
  <li>utf-16</li>
  <li>UTF-16</li>
  </ul>
  
  <hr>
  
  <p>Should the charset be specified in <code>Response.ContentEncoding</code>?</p>
  
  <pre class="lang-c# prettyprint-override"><code>protected void Page_Load(object sender, EventArgs e)
  {
     String xml = "&lt;foo&gt;Hello, world!&lt;/foo&gt;";
  
     Response.Clear();
     Response.ContentType = "text/xml";
     Response.ContentEncoding = Encoding.UTF8;
     Response.Write(xml);
     Response.End();
  }
  </code></pre>
  
  <p>Is using <code>Response.ContentEncoding</code> better than jamming it into <code>Response.ContentType</code>? Is it worse? Is the former supported? Is the latter?</p>
  
  <hr>
  
  <p>I don't actually want to write a String out; I want to write out an <code>XmlDocument</code>. <a href="https://stackoverflow.com/questions/374535/how-to-return-xml-in-aspnet">Someone suggests I can use the <code>XmlWriter</code></a>:</p>
  
  <pre class="lang-c# prettyprint-override"><code>protected void Page_Load(object sender, EventArgs e)
  {
     XmlDocument xml = GetXmlDocumentToShowTheUser();
  
     Response.Clear();
     Response.ContentType = "text/xml";
     Response.ContentEncoding = Encoding.UTF8;
  
     using (TextWriter textWriter = new StreamWriter(
           Response.OutputStream,
           Encoding.UTF8))
     {
         XmlTextWriter xmlWriter = new XmlTextWriter(textWriter);
         // Write XML using xmlWriter
         //TODO: How to do this?
     }
  }
  </code></pre>
  
  <p>Note the use of <code>Response.OutputStream</code>, rather than <code>Response.Write</code>. Is this good? Bad? Better? Worse? Faster? Slower? More memory intensive? Less memory intensive?</p>
  
  <hr>
  
  <p>I <a href="http://www.deez.info/sengelha/2006/02/06/how-return-xml-from-aspx-in-aspnet-11/#comment-24670" rel="noreferrer">read</a> that you should render</p>
  
  <blockquote>
    <p>the XML in the page’s Render() method
    to avoid problems with chunking
    encountered when using Page_Load().</p>
  </blockquote>
  
  <p>What is <strong>chunking</strong>?
  What are the problems with chunking, and how does using using <code>Page_Render</code> eliminate them?</p>
  
  <hr>
  
  <p>I don't want to write the contents of my <code>XmlDocument</code> object into a string and then write that because that wastes memory. That is, any of these would be bad:</p>
  
  <pre class="lang-c# prettyprint-override"><code>Response.Write(doc.ToString());
  Response.Write(doc.InnerXml);
  xmlWrite.WriteString(doc.ToString());
  xmlWrite.WriteString(doc.InnerXml);
  </code></pre>
  
  <hr>
  
  <p><strong>Similar Questions</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/374535/how-to-return-xml-in-aspnet">How to return XML in ASP.NET</a></p>
  
  <p><strong>References</strong></p>
  
  <p><a href="http://www.deez.info/sengelha/2006/02/06/how-return-xml-from-aspx-in-aspnet-11" rel="noreferrer">How Return XML From ASPX in ASP.NET 1.1</a></p>
  
  <p><a href="http://forums.asp.net/t/1298093.aspx" rel="noreferrer">Writing XML output to an ASP.NET webpage</a></p>
  
  <p><a href="http://bytes.com/groups/net-asp/647876-how-do-you-output-xml-asp-net" rel="noreferrer">How do you output XML from ASP.NET?</a></p>
  
  <p><a href="http://www.aspcode.net/Creating-an-ASHX-handler-in-ASPNET.aspx" rel="noreferrer">Creating an ASHX handler in ASP.NET</a></p>