title: .NET: Posting Xml to a web-server?
body: <p>What is the proper way to post an <a href="http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx" rel="nofollow noreferrer">XmlDocument</a> to a web-server? Here is the skeleton function:</p>
  
  <pre><code>public static void PostXml(XmlDocument doc, String url)
  { 
     //TODO: write this
  }
  </code></pre>
  
  <hr>
  
  <p>Right now i use: </p>
  
  <pre><code>//Warning: Do not use this PostXml implmentation
  //It doesn't adjust the Xml to match the encoding used by WebClient
  public static void PostXml(XmlDocument doc, String url)
  {
     using (WebClient wc = new WebClient())
     {
        wc.UploadString(url, DocumentToStr(doc));
     }
  }
  </code></pre>
  
  <p>Where <code>DocumentToStr</code> is a perfectly valid and correct method:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Convert an XmlDocument to a String
  /// &lt;/summary&gt;
  /// &lt;param name="doc"&gt;The XmlDocument to be converted to a string&lt;/param&gt;
  /// &lt;returns&gt;The String version of the XmlDocument&lt;/returns&gt;
  private static String DocumentToStr(XmlDocument doc)
  {
      using (StringWriter writer = new StringWriter())
      {
         doc.Save(writer);
         return writer.ToString();
      }
  }
  </code></pre>
  
  <p>The problem with my implementation of <code>PostXml</code> is that it posts the String <em>exactly</em> as is. This means that (in my case) the http request is:</p>
  
  <pre><code>POST https://stackoverflow.com/upload.php HTTP/1.1
  Host: stackoverflow.com
  Content-Length: 557
  Expect: 100-continue
  
  &lt;?xml version="1.0" encoding="utf-16"?&gt;
  &lt;AccuSpeedData MACAddress="00252f21279e" Date="2010-10-07 10:49:41:768"&gt;
    &lt;Secret SharedKey="1234567890abcdefghijklmnopqr" /&gt;
    &lt;RegisterSet TimeStamp="2010-10-07 10:49:41:768"&gt;
      &lt;Register Address="total:power" Type="Analog" Value="485" /&gt;
      &lt;Register Address="total:voltage" Type="Analog" Value="121.4" /&gt;
      &lt;Register Address="total:kVA" Type="Analog" Value="570" /&gt;
    &lt;/RegisterSet&gt;
  &lt;/AccuSpeedData&gt;
  </code></pre>
  
  <p>You'll notice that the xml declaration has an incorrect encoding:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
  </code></pre>
  
  <p>The <a href="http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx" rel="nofollow noreferrer"><code>WebClient</code></a> is not sending the request in <code>utf-16</code> unicode, that was how Strings in .NET are stored. i don't even <strong>know</strong> the encoding used by the <a href="http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx" rel="nofollow noreferrer"><code>WebClient</code></a>.</p>
  
  <hr>
  
  <p>The http post of the xml needs to be properly encoded, which normally happens during a call to:</p>
  
  <pre><code>Save(textWriter)
  </code></pre>
  
  <p>During a call to <a href="http://msdn.microsoft.com/en-us/library/sa5wd18z.aspx" rel="nofollow noreferrer"><code>Save</code></a> the <a href="http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx" rel="nofollow noreferrer"><code>XmlDocument</code></a> object will adjust the xml-declaration based on <a href="http://msdn.microsoft.com/en-us/library/system.io.textwriter.encoding.aspx" rel="nofollow noreferrer"><code>Encoding</code></a> of the <a href="http://msdn.microsoft.com/en-us/library/system.io.textwriter.aspx" rel="nofollow noreferrer"><code>TextWriter</code></a> it is being asked to save to. Unfortunately <code>WebClient</code> doesn't expose a <code>TextWriter</code> that i can save the XmlDocument to.</p>
  
  <h2>See also</h2>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/1732496/post-xml-to-net-web-service">Post XML to .net web service</a></li>
  <li><a href="https://stackoverflow.com/questions/984818/http-post-of-xml-string-and-save-it-as-xml-on-server-django-gae">HTTP Post of XML string and save it as .xml on server (django/GAE)</a></li>
  <li><a href="https://stackoverflow.com/questions/1099021">Send XML via HTTP Post to IP:Port</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/library/sa5wd18z.aspx" rel="nofollow noreferrer">MSDN: XmlDocument.Save Method (TextWriter)</a></li>
  <li><a href="https://stackoverflow.com/questions/1183691/sending-gzipped-data-in-webrequest">Sending gzipped data in WebRequest?</a></li>
  <li><a href="https://stackoverflow.com/questions/2962155">C# web request with POST encoding question</a></li>
  <li><a href="https://stackoverflow.com/questions/2252762/getrequeststream-throws-timeout-exception-randomly">GetRequestStream throws Timeout exception randomly</a></li>
  <li><a href="http://devproj20.blogspot.com/2008/02/writing-xml-with-utf-8-encoding-using.html" rel="nofollow noreferrer">Writing XML with UTF-8 Encoding using XmlTextWriter and StringWriter</a></li>
  </ul>