Wednesday, 12 October 2016

Serialize dataset to xml in webservice .net

Create a class for extension:

public static class Extensions
    {
        public static string ToXml(this DataSet ds)
        {
            using (var mStream = new MemoryStream())
            {
                using (TextWriter sWriter = new StreamWriter(mStream))
                {
                    var xmlSerializer = new XmlSerializer(typeof(DataSet));
                    xmlSerializer.Serialize(sWriter, ds);
                    return Encoding.UTF8.GetString(mStream.ToArray());
                }
            }
        }
    }

1 comment: