using System.Text;
using System.Xml.Serialization;

namespace CloudBuilder.Util
{
    public class SerializeHelper
    {     
        public static string Serialize(object o)
        {
            if (o == null) return null;

            XmlSerializer serializer = new XmlSerializer(o.GetType());           

            using (MemoryStream stream = new MemoryStream())
            {
                StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
                serializer.Serialize((TextWriter)writer, o);
                stream.Seek(0L, SeekOrigin.Begin);
                StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                return reader.ReadToEnd();
            }
        }                

        public static void WriteAllText(string filePath, object o)
        {
            File.WriteAllText(filePath, Serialize(o));
        }
    }
}
