XML : How to read back XML data from memory stream

I want to write an object (using XmlDictionaryWriter) to memory stream and read it back. I have the following code. When I read back the object from Memory Stream it crashes.

     [DataContract]      public class FullName      {          [DataMember]          public string FirstName { get; set; }                        [DataMember]          public string LastName { get; set; }      }      class Program      {          static void Main(string[] args)          {              FullName fn = new FullName();              fn.LastName = "John";              fn.LastName = "Doe";              MemoryStream ms = Process(fn);              Console.WriteLine("Read Back");              ms.Position = 0;              DataContractSerializer serializer = new DataContractSerializer(typeof(FullName));              FullName fn2 = (FullName)serializer.ReadObject(ms); //<--- crash here                    Console.WriteLine("Deserialized record: {0} {1}", fn2.FirstName, fn2.LastName);          }          public static MemoryStream Process(FullName name)          {              var ms = new MemoryStream();              var binary = XmlDictionaryWriter.CreateBinaryWriter(ms);              var ser = new DataContractSerializer(typeof(FullName));              ser.WriteObject(binary, name);              binary.Flush();              Console.WriteLine("MemoryStream is {0} bytes long", ms.Length);              return ms;          }      }    

No comments:

Post a Comment