Convert List or any type of object to XML string data in C#

Convert List or any type of object to XML string data in C#

Here is a method to conver any type of object to string xml, this method will take parameters of T type and will return XML string.

public static string ConvertToXML<T>(T obj)
        {
            try
            {

                using (StringWriter stringWriter = new StringWriter(new StringBuilder()))
                {
                    System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                    xmlSerializer.Serialize(stringWriter, obj);
                    XDocument xDoc = XDocument.Parse(stringWriter.ToString());
                    xDoc.Declaration = null;
                    return xDoc.ToString();
                }

            }
            catch (Exception ex)
            {
                return ex;
            }
        }