/// <summary> /// XML-serializes an object into a binary stream. /// </summary> public static void SerializeBin <T>(T obj, BinaryWriter w) { MemoryStream ms = new MemoryStream(10000); XmlWriterSettings ws = new XmlWriterSettings { Indent = false }; using (TextWriter tw = new StreamWriter(ms)) { XmlSerializerExt.Serialize(obj, tw, ws); } byte[] xmlData = ms.ToArray(); w.Write(xmlData.Length); w.Write(xmlData); }
/// <summary> /// XML-deserializes an object from a binary stream. /// </summary> public static T DeserializeBin <T>(BinaryReader r) { Int32 xmlDataLength = r.ReadInt32(); byte[] xmlData = new byte[xmlDataLength]; r.Read(xmlData, 0, xmlDataLength); MemoryStream ms = new MemoryStream(xmlData); T obj; using (TextReader tr = new StreamReader(ms)) { obj = XmlSerializerExt.Deserialize <T>(tr); } return(obj); }