title: Preserving character encoding when writing XElement to Stream
body: <p>I'm working on an ASP.NET MVC application.  There's a service which I need to send RESTful requests to.  In this particular case, I have to use a POST to send XML to the service.  We are not using WCF.</p>
  
  <p>The XML is in an <code>XElement</code> object.  The original XML includes character encoding information.  I want to keep the character encoding unchanged when I write the XML to the service.</p>
  
  <p>I can get a reference to the request stream using code like this:</p>
  
  <pre><code>HttpWebRequest req = (HttpWebRequest) WebRequest.Create( url );
  req.Method = "POST";
  req.Timeout = 30000;
  Stream requestStream = req.GetRequestStream();
  </code></pre>
  
  <p>I've written this code:</p>
  
  <pre><code>using ( XmlWriter writer = new XmlTextWriter( requestStream, Encoding.UTF8 ) ) {
      xml.WriteTo( writer );
  }
  </code></pre>
  
  <p>But this may change the encoding of the XML.  It's important that the encoding doesn't change.  I can't seem to find a property or method that returns the encoding of the <code>XElement.</code></p>
  
  <p>How do I write the <code>XElement</code> to the <code>requestStream</code> and preserve the existing character encoding?</p>