Serialize the Data to XML


public static String SerializeInterface(object SerializedObject)
       {
           using (MemoryStream stream = new MemoryStream())
           {
               XmlSerializer serializer = null;
               if (SerializedObject.GetType() == typeof(INTERFACE_ERRORS))
               {
                   serializer = new XmlSerializer(typeof(INTERFACE_ERRORS));
                   INTERFACE_ERRORS o = (INTERFACE_ERRORS)SerializedObject;
                   serializer.Serialize((Stream)stream, o);
               }
               else if (SerializedObject.GetType() == typeof(DETAILS_TO_SUB_SYSTEM))
               {
                   serializer = new XmlSerializer(typeof(DETAILS_TO_SUB_SYSTEM));
                   DETAILS_TO_SUB_SYSTEM details_to_sub_system = (DETAILS_TO_SUB_SYSTEM)SerializedObject;
                   serializer.Serialize((Stream)stream, details_to_sub_system);
               }

               stream.Seek(0L, SeekOrigin.Begin);
               byte[] buffer = new byte[stream.Length];
               int count = stream.Read(buffer, 0, 20);
               while (count < stream.Length)
               {
                   buffer[count++] = Convert.ToByte(stream.ReadByte());
               }

               char[] chars = new char[Encoding.UTF8.GetCharCount(buffer, 0, count)];
               Encoding.UTF8.GetDecoder().GetChars(buffer, 0, count, chars, 0);
               return new string(chars);
           }
       }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s