当前位置: 移动技术网 > IT编程>开发语言>.net > XML序列化

XML序列化

2018年10月18日  | 移动技术网IT编程  | 我要评论

郑雪儿三级,快乐大本营091017,丰职

#region  序列化

        /// <summary>
        /// xml序列化
        /// </summary>
        /// <param name="obj">序列对象</param>
        /// <param name="filepath">xml文件路径</param>
        /// <returns>是否成功</returns>
        public static bool serializetoxml(object obj, string filepath)
        {
            bool result = false;

            filestream fs = null;
            try
            {
                fs = new filestream(filepath, filemode.create, fileaccess.write, fileshare.readwrite);
                xmlserializer serializer = new xmlserializer(obj.gettype());
                serializer.serialize(fs, obj);
                result = true;
            }
            catch (exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                    fs.close();
            }
            return result;

        }

        /// <summary>
        /// xml反序列化
        /// </summary>
        /// <param name="type">目标类型(type类型)</param>
        /// <param name="filepath">xml文件路径</param>
        /// <returns>序列对象</returns>
        public static object deserializefromxml(type type, string filepath)
        {
            filestream fs = null;
            try
            {
                fs = new filestream(filepath, filemode.open, fileaccess.read, fileshare.readwrite);
                xmlserializer serializer = new xmlserializer(type);
                return serializer.deserialize(fs);
            }
            catch (exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                    fs.close();
            }
        }

        #endregion

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网