Serialize plain clean XML in .NET

Posted by Jon Canning on Geeks with Blogs See other posts from Geeks with Blogs or by Jon Canning
Published on Tue, 12 Jun 2012 14:40:43 GMT Indexed on 2012/06/12 16:41 UTC
Read the original article Hit count: 272

Filed under:
        public static string ToXml<T>(this T obj) where T : class
        {
            using (var stringWriter = new StringWriter())
            {
                var xmlWriterSettings = new XmlWriterSettings { OmitXmlDeclaration = true };
                using (var xmlWriter = XmlWriter.Create(stringWriter, xmlWriterSettings))
                {
                    var xmlSerializerNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
                    var xmlSerializer = new XmlSerializer(typeof(T));
                    xmlSerializer.Serialize(xmlWriter, obj, xmlSerializerNamespaces);
                }
                return stringWriter.ToString();
            }
        }

© Geeks with Blogs or respective owner