Note that there are some explanatory texts on larger screens.

plurals
  1. POprotobuf-net deserializing error Invalid tag: 0
    text
    copied!<p>I want to serialize using protobuf-net to string and deserialize back to object. It works fine if I serialize to file like .bin However the code below throws an exception while deserializing. Any ideas?</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Runtime.Serialization; using ProtoBuf; namespace Proto { class Program { public long MemUsed = 0; NetDataContractSerializer ser = new NetDataContractSerializer(); static void Main(string[] args) { var person = new Person { Id = 12345, Name = "Fred", Address = new Address { Line1 = "Flat 1", Line2 = "The Meadows" } }; Console.WriteLine("Serializing ....."); var result = Serialize&lt;Person&gt;(person); Console.WriteLine(" Serialization Complete ....."); Console.WriteLine("Press key to Deserialize ...."); Console.ReadKey(); var person2 = new Person(); DeSerialize&lt;Person&gt;(ref person2,result); Console.WriteLine(person2.Name); Console.ReadLine(); } public static string Serialize&lt;T&gt;(T myObj) { string retVal = ""; using (MemoryStream memStream = new MemoryStream()) { Serializer.Serialize&lt;T&gt;(memStream, myObj); memStream.Position = 0; retVal = new StreamReader(memStream).ReadToEnd(); } return (retVal); } public static void DeSerialize&lt;T&gt;(ref T myObj, string xmlString) { byte[] byteArray = Encoding.ASCII.GetBytes(xmlString); MemoryStream stream = new MemoryStream(byteArray,0,byteArray.Length); stream.Capacity = Convert.ToInt32(stream.Length); myObj = Serializer.Deserialize&lt;T&gt;(stream); stream.Close(); } } [ProtoContract] class Person { [ProtoMember(1)] public int Id {get;set;} [ProtoMember(2)] public string Name { get; set; } [ProtoMember(3)] public Address Address {get;set;} } [ProtoContract] class Address { [ProtoMember(1)] public string Line1 {get;set;} [ProtoMember(2)] public string Line2 {get;set;} } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload