title: Root Element Missing when deserializing an XML response using XmlSerializer
body: <p>I have the following XML response I am trying to deserialize using XmlSerializer. When I remove the call to XML serializer I throw no errors. Every time I use XmlSerializer I get
  an exception. What am I missing?</p>
  
  <p>Exception is:    </p>
  
  <pre><code>System.Xml.XmlException: Root element is missing.
    at System.Xml.XmlTextReaderImpl.Throw(Exception e)
    at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
    at System.Xml.XmlTextReaderImpl.Read()
    at System.Xml.XmlTextReader.Read()
    at System.Xml.XmlReader.MoveToContent()
    at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderSubmitReportResponse.Read5_NeweggAPIResponse()
  </code></pre>
  
  <p>Xml Doc is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;NeweggAPIResponse&gt;
      &lt;IsSuccess&gt;true&lt;/IsSuccess&gt;
      &lt;OperationType&gt;OrderListReportResponse&lt;/OperationType&gt;
      &lt;SellerID&gt;myID&lt;/SellerID&gt;
      &lt;ResponseBody&gt;
          &lt;ResponseList&gt;
              &lt;ResponseInfo&gt;
                  &lt;RequestId&gt;XXXXXXXX&lt;/RequestId&gt;
                  &lt;RequestType&gt;ORDER_LIST_REPORT&lt;/RequestType&gt;
                  &lt;RequestDate&gt;07/26/2012 09:27:06&lt;/RequestDate&gt;
                  &lt;RequestStatus&gt;SUBMITTED&lt;/RequestStatus&gt;
              &lt;/ResponseInfo&gt;
          &lt;/ResponseList&gt;
      &lt;/ResponseBody&gt;
  &lt;/NeweggAPIResponse&gt;
  </code></pre>
  
  <p>My call to XmlSerializer is: </p>
  
  <pre><code>XmlSerializer serializer = new XmlSerializer(typeof(SubmitReportResponse));
  reportReq = serializer.Deserialize(respStream) as SubmitReportResponse;
  </code></pre>
  
  <p>SubmitReportResponse class is: </p>
  
  <pre><code>    public enum RequestStatus
  {
      ALL,
      SUBMITTED,
      IN_PROGRESS,
      FINISHED,
      CANCELLED
  }
  
  /// &lt;summary&gt;
  /// TODO: Update summary.
  /// &lt;/summary&gt;
  [XmlRoot("NeweggAPIResponse")]
  public class SubmitReportResponse
  {
      public string IsSuccess { get; set; }
      public string OperationType { get; set; }
      public string SellerID { get; set; }
      public ReportResponseBody ResponseBody { get; set; }
  
      public SubmitReportResponse()
      {
          ResponseBody = new ReportResponseBody();
      }
  }
  
  public class ReportResponseBody
  {
      public string Memo { get; set; }
      public ReportResponseList[] ResponseList { get; set; }   
  
  
  
      public ReportResponseBody()
      {
  
          ResponseList = new ReportResponseList[0];
      }
  }
  
  public class ReportResponseList
  {
      public ResponseInfo[] ResponseInfo { get; set; }
  
      public ReportResponseList()
      {
          ResponseInfo = new ResponseInfo[0];
      }
  
  }
  
  public class ResponseInfo
  {
      public string RequestId { get; set; }
      public string RequestType { get; set; }
      public string RequestDate { get; set; }
      public RequestStatus RequestStatus { get; set; }
  
      public ResponseInfo()
      {
          RequestStatus = new RequestStatus();
      }
  }
  </code></pre>
  
  <p>EDIT: </p>
  
  <p>Requesting Code: </p>
  
  <pre><code>            HttpWebRequest request = WebRequest.Create(endpoint) as HttpWebRequest;
              request.Proxy = null;
              request.Method = "POST";
              //Specify the xml/Json content types that are acceptable. 
              request.ContentType = "application/xml";
              request.Accept = "application/xml";
  
              //Attach authorization information
              request.Headers.Add("Authorization", apikey);
              request.Headers.Add("Secretkey", secretkey);
  
              GetOrderListRequest requestObj = new GetOrderListRequest();
              requestObj.OperationType = OperationType.OrderListReportRequest;
              requestObj.RequestBody = new OrderListRequestBody();
              requestObj.RequestBody.OrderReportCriteria = new OrderReportCriteria();
              requestObj.RequestBody.OrderReportCriteria.Status = 3;
              requestObj.RequestBody.OrderReportCriteria.KeywordsType = 0;
              requestObj.RequestBody.OrderReportCriteria.OrderDateFrom = "2012-01-01";
              requestObj.RequestBody.OrderReportCriteria.OrderDateTo = "2012-07-26";
              requestObj.RequestBody.OrderReportCriteria.OrderDownloaded = "false";
  
              string requestBody = SerializeToString(requestObj);
  
              byte[] byteStr = Encoding.UTF8.GetBytes(requestBody);
              request.ContentLength = byteStr.Length;
  
              using (Stream stream = request.GetRequestStream())
              {
                  stream.Write(byteStr, 0, byteStr.Length);
              }
  
              //Parse the response
              using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
              {
                  //Business error
                  if (response.StatusCode != HttpStatusCode.OK)
                  {
                      Console.WriteLine(string.Format("Error: response status code is{0}, at time:{1}", response.StatusCode, DateTime.Now.ToString()));
  
                      return;
                  }
                  else if (response.StatusCode == HttpStatusCode.OK)//Success
                  {
                      using (Stream respStream = response.GetResponseStream())
                      {
                          StreamReader readerOK = new StreamReader(respStream);
                          //Console.WriteLine(String.Format("Result:{0}", DateTime.Now.ToString()));
                          Console.WriteLine(String.Format("{0}", readerOK.ReadToEnd()));
  
                          XmlSerializer serializer = new XmlSerializer(typeof(SubmitReportResponse));
                          reportReq = serializer.Deserialize(respStream) as SubmitReportResponse;
                      }
                  }
              }
  
  
      public string SerializeToObj(object obj)
      {
  
          XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
          ns.Add("", "");
          XmlWriterSettings settings = new XmlWriterSettings();
          settings.OmitXmlDeclaration = true;
          settings.Encoding = new UTF8Encoding(false);
          settings.Indent = true;
          XmlSerializer xs = new XmlSerializer(obj.GetType());
          MemoryStream ms = new MemoryStream();
  
          // xs.Serialize(ms, obj,ns);
  
  
          XmlWriter writer = XmlWriter.Create(ms, settings);
          xs.Serialize(writer, obj, ns);
  
          return Encoding.UTF8.GetString(ms.ToArray());
      }
  </code></pre>
  
  <p>Resolution: </p>
  
  <p>It seems that calling <code>Console.WriteLine(String.Format("{0}", readerOK.ReadToEnd()));</code> causes the stream to be consumed and be unavailable for serialization. Removing this line allowed me to properly serialize the XML into my classes. </p>