Serialize To Xml String - CSharp System.Xml

CSharp examples for System.Xml:XML Serialization

Description

Serialize To Xml String

Demo Code


using System.Xml.Serialization;
using System.Xml;
using System.Text;
using System.IO;/*w w w  . j  av a2s .c  o  m*/
using System;

public class Main{
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeToXmlString(object obj)
        {
            var writer = new StringWriter(new StringBuilder());
            new XmlSerializer(obj.GetType()).Serialize(writer, obj);
            string str = writer.ToString().Replace("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", "");
            str = str.Replace("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "");
            str = str.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "").Trim();
            return str;
        }
}

Related Tutorials