• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
梦幻仙境的精灵
博客园    首页    新随笔    联系   管理    订阅  订阅

实体类和xml之间的序列化

    /// <summary>
    /// Xml序列化与反序列化
    /// </summary>
    public class XmlUtil
    {
        public static string GetRoot(string xml)
        {
            if (xml == null)
                xml = "";//预防下边的 Replace()、Trim()报错
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml.Replace("\r\n", "").Replace("\0", "").Trim());
            var e = doc.DocumentElement;
            return e.InnerText;
        }

        #region 反序列化
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="xml">XML字符串</param>
        /// <returns></returns>
        public static T Deserialize<T>(string xml)
        {
            return (T)Deserialize(typeof(T), xml);
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="stream">字节流</param>
        /// <returns></returns>
        public static T Deserialize<T>(Stream stream)
        {
            return (T)Deserialize(typeof(T), stream);
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="xml">XML字符串</param>
        /// <returns></returns>
        public static object Deserialize(Type type, string xml)
        {
            if (xml == null)
                xml = "";//预防下边的 Replace()、Trim()报错
            try
            {
                xml = xml.Replace("\r\n", "").Replace("\0", "").Trim();
                using (StringReader sr = new StringReader(xml))
                {
                    XmlSerializer xmldes = new XmlSerializer(type);
                    return xmldes.Deserialize(sr);
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="type"></param>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static object Deserialize(Type type, Stream stream)
        {
            XmlSerializer xmldes = new XmlSerializer(type);
            return xmldes.Deserialize(stream);
        }
        #endregion
        #region 序列化
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static string Serializer<T>(T obj)
        {
            return Serializer(typeof(T), obj);
        }
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static string Serializer(Type type, object obj)
        {
            MemoryStream Stream = new MemoryStream();
            XmlSerializerNamespaces _name = new XmlSerializerNamespaces();
            _name.Add("", "");//这样就 去掉 attribute 里面的 xmlns:xsi 和 xmlns:xsd
            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
            xmlWriterSettings.Encoding = new UTF8Encoding(false);//设置编码,不能用Encoding.UTF8,会导致带有BOM标记 
            xmlWriterSettings.Indent = true;//设置自动缩进
            //xmlWriterSettings.OmitXmlDeclaration = true;//删除XmlDeclaration:<?xml version="1.0" encoding="utf-16"?>
            //xmlWriterSettings.NewLineChars = "\r\n";
            //xmlWriterSettings.NewLineHandling = NewLineHandling.None;
            XmlSerializer xml = new XmlSerializer(type);
            try
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(Stream, xmlWriterSettings))
                {
                    //序列化对象
                    xml.Serialize(xmlWriter, obj, _name);
                }
            }
            catch (InvalidOperationException)
            {
                throw;
            }
            return Encoding.UTF8.GetString(Stream.ToArray()).Trim();
        }
        #endregion
    }

 

posted @ 2014-12-19 10:14  梦幻仙境的精灵  阅读(554)  评论(1)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3