Surendra Sharma

Surendra Sharma

Search This Blog

Wednesday, June 5, 2013

Serialize object to XML

        /// <summary>
        /// Serialize object to XML
        /// </summary>
        /// <param name="instance">Object Instance to be serialized</param>
        /// <returns>Respective XML for specific instace</returns>
        public static string SerializeToXml(object instance) {
            string serializedXml = string.Empty;

            if (instance != null) {
                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter();
                XmlSerializer serializer = new XmlSerializer(instance.GetType(), "");
                serializer.Serialize(sw, instance);
                serializedXml = sw.ToString();
            }

            return serializedXml;

        }

No comments:

Post a Comment