title: Using DataContractSerializer to serialize, but can't deserialize back
body: <p>I have the following 2 functions:</p>
  
  <pre><code>public static string Serialize(object obj)
  {
      DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
      MemoryStream memoryStream = new MemoryStream();
      serializer.WriteObject(memoryStream, obj);
      return Encoding.UTF8.GetString(memoryStream.GetBuffer());
  }
  
  public static object Deserialize(string xml, Type toType)
  {
      MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
     // memoryStream.Position = 0L;
      XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, Encoding.UTF8, new XmlDictionaryReaderQuotas(), null);
      DataContractSerializer dataContractSerializer = new DataContractSerializer(toType);
      return dataContractSerializer.ReadObject(reader);
  }
  </code></pre>
  
  <p>The first one seems to serialize an object to an xml string just fine.  The XML appears valid, no broken tags, no white spaces at the beginning or at the end, etc.  Now the second function doesn't want to deserialize my xml string back to the object.  On the last line I get:</p>
  
  <blockquote>
    <p>There was an error deserializing the
    object of type [MY OBJECT TYPE HERE].
    The data at the root level is invalid.
    Line 1, position 1.</p>
  </blockquote>
  
  <p>What am I doing wrong?  I tried rewriting the Deserialize function a few times, and it always seems to be the same kind of error.  Thank you!</p>
  
  <p>Oh, and this is how I'm calling the 2 functions:</p>
  
  <pre><code>SomeObject so = new SomeObject();
  string temp = SerializationManager.Serialize(so);
  so = (SomeObject)SerializationManager.Deserialize(temp, typeof(SomeObject));
  </code></pre>