static void Test() { MemoryStream serializedStream = new MemoryStream(); XmlBinaryReaderSession readerSession = MyReaderSession.CreateReaderSession(serializedStream); XmlDictionaryReader xdr = XmlDictionaryReader.CreateBinaryReader(serializedStream, null, XmlDictionaryReaderQuotas.Max, readerSession); DataContractSerializer dcs = new DataContractSerializer(typeof(List <Character>)); List <Character> result = (List <Character>)dcs.ReadObject(xdr); }
static void Main(string[] args) { List <Character> instance = new List <Character> { new Character { Name = "Shaggy", Age = 19, DateOfBirth = new DateTime(1991, 9, 13), Type = "Human" }, new Character { Name = "Scooby", Age = 10, DateOfBirth = new DateTime(2000, 5, 20), Type = "Dog" }, new Character { Name = "Scrappy", Age = 3, DateOfBirth = new DateTime(2008, 1, 2), Type = "Dog" }, new Character { Name = "Fred", Age = 22, DateOfBirth = new DateTime(1988, 5, 29), Type = "Human" }, new Character { Name = "Velma", Age = 24, DateOfBirth = new DateTime(1986, 5, 30), Type = "Human" }, new Character { Name = "Daphne", Age = 20, DateOfBirth = new DateTime(1990, 5, 1), Type = "Human" }, }; Func <Stream, XmlBinaryWriterSession, XmlDictionaryWriter> createTextXmlWriter = (s, xbws) => XmlDictionaryWriter.CreateTextWriter(s); Func <Stream, XmlBinaryWriterSession, XmlDictionaryWriter> createNoDictBinaryWriter = (s, xbws) => XmlDictionaryWriter.CreateBinaryWriter(s); Func <Stream, XmlBinaryWriterSession, XmlDictionaryWriter> createDictBinaryWriter = (s, xbws) => XmlDictionaryWriter.CreateBinaryWriter(s, null, xbws); Func <Stream, XmlBinaryReaderSession, XmlDictionaryReader> createTextXmlReader = (s, xbrs) => XmlDictionaryReader.CreateTextReader(s, XmlDictionaryReaderQuotas.Max); Func <Stream, XmlBinaryReaderSession, XmlDictionaryReader> createNoDictBinaryReader = (s, xbrs) => XmlDictionaryReader.CreateBinaryReader(s, XmlDictionaryReaderQuotas.Max); Func <Stream, XmlBinaryReaderSession, XmlDictionaryReader> createDictBinaryReader = (s, xbrs) => XmlDictionaryReader.CreateBinaryReader(s, null, XmlDictionaryReaderQuotas.Max, xbrs); List < Tuple < string, Func <Stream, XmlBinaryWriterSession, XmlDictionaryWriter>, Func <Stream, XmlBinaryReaderSession, XmlDictionaryReader> > > tests; tests = new List <Tuple <string, Func <Stream, XmlBinaryWriterSession, XmlDictionaryWriter>, Func <Stream, XmlBinaryReaderSession, XmlDictionaryReader> > >(); tests.Add( new Tuple <string, Func <Stream, XmlBinaryWriterSession, XmlDictionaryWriter>, Func <Stream, XmlBinaryReaderSession, XmlDictionaryReader> >( "Text/XML", createTextXmlWriter, createTextXmlReader)); tests.Add( new Tuple <string, Func <Stream, XmlBinaryWriterSession, XmlDictionaryWriter>, Func <Stream, XmlBinaryReaderSession, XmlDictionaryReader> >( "Binary/no dict", createNoDictBinaryWriter, createNoDictBinaryReader)); tests.Add( new Tuple <string, Func <Stream, XmlBinaryWriterSession, XmlDictionaryWriter>, Func <Stream, XmlBinaryReaderSession, XmlDictionaryReader> >( "Binary/with dict", createDictBinaryWriter, createDictBinaryReader)); foreach (var test in tests) { Console.WriteLine(test.Item1); bool needsDict = test.Item2 == createDictBinaryWriter; MemoryStream ms = new MemoryStream(); MyWriterSession writerSession = null; if (needsDict) { writerSession = new MyWriterSession(); } XmlDictionaryWriter xdw = test.Item2(ms, writerSession); Serialize(xdw, instance); if (needsDict) { MemoryStream newMs = new MemoryStream(); writerSession.SaveDictionaryStrings(newMs); ms.Position = 0; ms.CopyTo(newMs); ms = newMs; } Console.WriteLine(" Serialized, size = {0}", ms.Position); File.WriteAllBytes(@"c:\temp\serialized.bin", ms.ToArray()); ms.Position = 0; XmlBinaryReaderSession readerSession = null; if (needsDict) { readerSession = MyReaderSession.CreateReaderSession(ms); } XmlDictionaryReader xdr = test.Item3(ms, readerSession); List <Character> deserialized = Deserialize <List <Character> >(xdr); Console.WriteLine(" Deserialized: {0}", deserialized); } }