首页 > 代码库 > 序列化为xml

序列化为xml

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;        }

 

序列化为xml