private static string SerializeToXml(object obj, string encoding) { if (obj != null) { XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlSerializer serializer = new XmlSerializer(obj.GetType()); MemoryStream ms = new MemoryStream(); XmlTextWriter xtw = new XmlTextWriter(ms, System.Text.Encoding.GetEncoding(encoding)); xtw.Formatting = Formatting.Indented; serializer.Serialize(xtw, obj, ns); ms.Seek(0, SeekOrigin.Begin); StreamReader sr = new StreamReader(ms, System.Text.Encoding.GetEncoding(encoding)); string str = sr.ReadToEnd(); xtw.Close(); ms.Close(); return str; } return string.Empty; }