title: protobuf-net Serialize To String and Store in Database Then De Serialize
body: <p>I'd like to serialize/de-serialize an object using a string.  Just to note, when I serialize/de-serialize to a file everything works fine.  What I'm trying to do is get a string so I can store it in the database and then pull it out later to de-serialize.</p>
  
  <p>Here is code that works:</p>
  
  <pre><code>MemoryStream msTest = new MemoryStream();
  Serializer.Serialize(msTest, registrationBlocks);
  msTest.Position = 0;
  List&lt;RVRegistrationBlock&gt; CopiedBlocks = new List&lt;RVRegistrationBlock&gt;();
  CopiedBlocks = Serializer.Deserialize&lt;List&lt;RVRegistrationBlock&gt;&gt;(msTest);
  </code></pre>
  
  <p>The "CopiedBlocks" object is the same list that was in "registrationBlocks"  Works great, everything serialized/de-serialized.  I'm keeping everything in streams here.</p>
  
  <p>Here is the code that doesn't work when I try to get a string involved:</p>
  
  <pre><code>MemoryStream msTestString = new MemoryStream();
  Serializer.Serialize(msTestString, registrationBlocks);
  
  
  msTestString.Position = 0;
  StreamReader srRegBlock = new StreamReader(msTestString);
  
  byte[] bytedata64 = System.Text.Encoding.Default.GetBytes(srRegBlock.ReadToEnd());
  
  string stringBase64 = Convert.ToBase64String(bytedata64);
  
  byte[] byteAfter64 = Convert.FromBase64String(stringBase64);
  MemoryStream afterStream = new MemoryStream(byteAfter64);
  
  
  List&lt;RVRegistrationBlock&gt; CopiedBlocksString = new List&lt;RVRegistrationBlock&gt;();
  CopiedBlocksString = Serializer.Deserialize&lt;List&lt;RVRegistrationBlock&gt;&gt;(afterStream);
  </code></pre>
  
  <p>On the last line when it goes to deserialize, I get an exception:  Exception of type 'ProtoBuf.ProtoException' was thrown.  I can't drill into it, inner exception is null.  I can't figure out why it's doing it.</p>
  
  <p>I've definitely narrowed it down to the fact that when I get a string involved it goes haywire.  I'm storing the string in a database in an nvarchar(max) which is why I want the string.</p>
  
  <p>Any help would be super appreciated!</p>