[dba-VB] FW: JSON and XML in memory serialization sample code (Part 3.2)

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Tue Oct 13 15:17:52 CDT 2009


 

Continued (from 3.1).

 

    /// <summary>

    /// In memory object data serialization 

    /// </summary>

    public class InMemoryObjectsSerializer

    {

        /// <summary>

        /// Cconverts a custom object instance to XML string

        /// </summary>

        /// <param name="pObject">Object that is to be serialized to
XML</param>

        /// <returns>XML string in UTF8 format</returns>

        public static String Serialize<T>(Object pObject)

        {

            String XmlizedString = null;

            MemoryStream memoryStream = new MemoryStream();

            XmlSerializer xs = new XmlSerializer(typeof(T));

            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,
Encoding.UTF8);

 

            xs.Serialize(xmlTextWriter, pObject);

            memoryStream = (MemoryStream)xmlTextWriter.BaseStream;

            XmlizedString =
StringToBytes.UTF8ByteArrayToString(memoryStream.ToArray());

            return XmlizedString;

        }

 

        /// <summary>

        /// Reconstruct an object instance from XML string

        /// </summary>

        /// <param name="pXmlizedString">Xml string to be used for object
instance deserialization</param>

        /// <returns></returns>

        public static Object Deserialize<T>(String pXmlizedString)

        {

            XmlSerializer xs = new XmlSerializer(typeof(T));

            MemoryStream memoryStream = new
MemoryStream(StringToBytes.StringToUTF8ByteArray(pXmlizedString));

            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,
Encoding.UTF8);

            return xs.Deserialize(memoryStream);

        }

 

    }

 

 

}

 




More information about the dba-VB mailing list