title: c# object to xml, rest service
body: <p>I have been having problems with the 400 Bad Response, and have got it down to the XML sting that is getting passed. The string that works is</p>
  
  <pre><code>&lt;Data xmlns=\"http://www.eysnap.com/mPlayer\"&gt;
  &lt;Name&gt;Chris&lt;/Name&gt;
  &lt;Age&gt;29&lt;/Age&gt;
  &lt;Period&gt;123&lt;/Period&gt;
  &lt;msg&gt;Why&lt;/msg&gt;
  &lt;/Data&gt;
  </code></pre>
  
  <p>However when I try and seralize it I get:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
  &lt;Data xmlns:Namespace="http://www.eysnap.com/mPlayer"&gt;
  &lt;Name&gt;Chris&lt;/Name&gt;
  &lt;Age&gt;29&lt;/Age&gt;
  &lt;Period&gt;123&lt;/Period&gt;
  &lt;msg&gt;Why&lt;/msg&gt;
  &lt;/Data&gt;
  </code></pre>
  
  <p>I know that is the correct formatting, but I either need to set the xml output to a bare view or I need to configure my service to accept the correct view. </p>
  
  <p>Client Post Code:</p>
  
  <pre><code>        StringBuilder s = new StringBuilder();
          StringWriter writter = new StringWriter(s);
  
          XmlWriterSettings settings = new XmlWriterSettings();
          settings.OmitXmlDeclaration=true;
  
          using(XmlWriter writer = XmlWriter.Create(writter,settings)){
          XmlSerializer x = new XmlSerializer(d.GetType());
          XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
          ns.Add("Namespace","http://www.eysnap.com/mPlayer");
          x.Serialize(writter,d,ns);
          }
  
          string invalid = s.ToString();
          string valid ="&lt;Data xmlns=\"http://www.eysnap.com/mPlayer\"&gt;&lt;Name&gt;Chris&lt;/Name&gt;&lt;Age&gt;29&lt;/Age&gt;&lt;Period&gt;12 Years&lt;/Period&gt;&lt;msg&gt;Fucking hell&lt;/msg&gt;&lt;/Data&gt;";
              req.ContentLength = valid.Length;
              var sw = new StreamWriter(req.GetRequestStream());
              sw.Write(valid);
              sw.Close();
  
              res = (HttpWebResponse) req.GetResponse();
              Stream responseStream = res.GetResponseStream();
              var streamReader = new StreamReader(responseStream);
  
              //Read the response into an xml document
              var soapResonseXmlDocument = new XmlDocument();
              soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd());
  
              //return only the xml representing the response details (inner request)
              TextBox1.Text = soapResonseXmlDocument.InnerXml;
              //Response.Write(soapResonseXMLDocument.InnerXml);
  </code></pre>
  
  <p>Rest Service Interface:</p>
  
  <pre><code>    [OperationContract]
      [WebInvoke(Method = "POST",
          ResponseFormat = WebMessageFormat.Xml,
          RequestFormat = WebMessageFormat.Xml,
          BodyStyle = WebMessageBodyStyle.Wrapped,
          UriTemplate = "auth")]
      ResponseData Auth(Data rData);
  </code></pre>
  
  <p>Thanks </p>
  
  <p>Chris</p>