XML to memory file

2011-02-25


Here is a sample about XML and memory file:

 /// <summary>
    /// Method to convert a custom Object to XML string
    /// </summary>
    /// <param name="pObject">Object that is to be serialized to XML</param>
    /// <returns>XML string</returns>

    public String SerializeObject ( Object pObject )
    {
        try
        {
            String XmlizedString = null;

            MemoryStream memoryStream = new MemoryStream ( );
            XmlSerializer xs = new XmlSerializer ( typeof ( Animal ) );
            XmlTextWriter xmlTextWriter = new XmlTextWriter ( memoryStream, Encoding.UTF8 );


 
            xs.Serialize ( xmlTextWriter, pObject );
            memoryStream = ( MemoryStream ) xmlTextWriter.BaseStream;
            XmlizedString = UTF8ByteArrayToString ( memoryStream.ToArray ( ) );
            return XmlizedString;
        }
        catch ( Exception e )
        {
            System.Console.WriteLine ( e );
            return null;
        }
    }

    /// <summary>
    /// Method to reconstruct an Object from XML string
    /// </summary>
    /// <param name="pXmlizedString"></param>
    /// <returns></returns>
    public Object DeserializeObject ( String pXmlizedString )
    {
        XmlSerializer xs = new XmlSerializer ( typeof ( Automobile ) );
        MemoryStream memoryStream = new MemoryStream ( StringToUTF8ByteArray ( pXmlizedString ) );
        XmlTextWriter xmlTextWriter = new XmlTextWriter ( memoryStream, Encoding.UTF8 );
 
        return xs.Deserialize ( memoryStream );
    }

Above code from dotnetjohn, about more details, please read their website.