title: C# REST API: xml as post data
body: <p>I'm working on a project which requires me to work with an REST API. The API expects a DTO as input parameter.</p>
  
  <pre><code> [OperationContract]
          [WebInvoke(Method = "POST", UriTemplate = "campaign/create?session={sessionkey}", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml)]
          [XmlSerializerFormat(Style = OperationFormatStyle.Document, Use = OperationFormatUse.Literal)]
          FeedbackDTO PersistCampaign(string sessionkey, CampaignDTO PaycentoCampaign);
  </code></pre>
  
  <p>I'm trying to send the data to this method with the following method:</p>
  
  <pre><code>public static HttpWebResponse DoHttpWebRequest(String url, String method, string data)
  {
      HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
      req.KeepAlive = false;
      req.ContentType = "application/xml";
      req.Method = method;
      if ((method.Equals("POST") || method.Equals("PUT")) &amp;&amp; data != null)
      {
          byte[] buffer = Encoding.UTF8.GetBytes(data);
          Stream PostData = req.GetRequestStream();
          PostData.Write(buffer, 0, buffer.Length);
          PostData.Close();
      }
      return req.GetResponse() as HttpWebResponse;
  }
  </code></pre>
  
  <p>After hours of debugging I found that the problem is caused by the DateTime proprties. I started to 'manually' create the XML and do property per property. </p>
  
  <pre><code> sb.Append("&lt;Startdate&gt;").Append(HttpUtility.HtmlEncode(Startdate.ToString(Helper.DATE_FORMAT_STRING))).Append("&lt;/Startdate&gt;");
  </code></pre>
  
  <p>I tried adding a format (ddMMyyyy) in the toString but it's still causing a bad request. How should I add a DateTime property to the XML so ASP.NET is able to parse it into the DTO?</p>
  
  <p>Stuff I tried:</p>
  
  <ul>
  <li>StartDate.ToLongDateString()</li>
  <li>StartDate.ToString()</li>
  <li>StartDate.toString("ddhhyyyy")</li>
  <li>StartDate.Ticks</li>
  </ul>