码迷,mamicode.com
首页 > 其他好文 > 详细

序列化为xml

时间:2014-11-10 11:17:35      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   sp   for   div   log   bs   

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

标签:style   blog   color   os   sp   for   div   log   bs   

原文地址:http://www.cnblogs.com/ysf123/p/4086417.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!