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

XML序列化方法

时间:2019-12-31 10:40:21      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:orm   XML   本地   load   creat   保存   try   serialize   读取   

 

XML序列化方法

    public class XmlHelper
    {

        public static string XmlSerialize<T>(T entity)
        {
            StringBuilder buffer = new StringBuilder();
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (TextWriter writer = new StringWriter(buffer))
            {
                serializer.Serialize(writer, entity);
            }
            return Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(buffer.ToString()));

        }

        public static T DeXmlSerialize<T>(string xmlString)
        {
            T cloneObject;

            StringBuilder buffer = new StringBuilder();
            buffer.Append(xmlString);

            XmlSerializer serializer = new XmlSerializer(typeof(T));

            using (TextReader reader = new StringReader(buffer.ToString()))
            {
                Object obj = serializer.Deserialize(reader);
                cloneObject = (T)obj;
            }

            return cloneObject;
        }

        /// <summary>
        /// 将类的对象序列化后保存到本地xml文件
        /// </summary>
        /// <param name="entityObj">一个对象</param>
        /// <param name="fileName">本地xml文件路径</param>
        public static void XmlSerialize(object entityObj, string fileName)
        {
            using (var stream = new FileStream(fileName, FileMode.Create))
            {
                var serializer = new System.Xml.Serialization.XmlSerializer(entityObj.GetType());
                serializer.Serialize(stream, entityObj);
            }
        }

        /// <summary>
        /// 读取序列化后保存在本地的xml文件成一个对象
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="entityType"></param>
        /// <returns></returns>   
        public static object XmlDeserialize(string filename, Type entityType)
        {
            object obj;
            using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                var serializer = new System.Xml.Serialization.XmlSerializer(entityType);
                obj = serializer.Deserialize(stream);
            }
            return obj;
        }

        /// <summary>
        /// 将对象序列化成二进制文件保存到本地
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="fileName">二进制文件路径</param>
        public static void BinSerialize(object obj, string fileName)
        {
            using (var strm = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
            {
                System.Runtime.Serialization.IFormatter fmt = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                fmt.Serialize(strm, obj);
                strm.Flush();
            }
        }

        /// <summary>
        /// 将序列化的二进制文件反序列化成一个对象
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static object BinDeserialize(string fileName)
        {
            object obj;
            using (Stream strm = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                System.Runtime.Serialization.IFormatter fmt = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                strm.Position = 0;
                strm.Seek(0, SeekOrigin.Begin);
                obj = fmt.Deserialize(strm);
            }
            return obj;
        }

        /// <summary>
        /// 格式化XML代码
        /// </summary>
        /// <param name="strXml">xml字符窜</param>
        /// <returns></returns>
        public static string FormatXml(string strXml)
        {
            var result = string.Empty;
            try
            {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.LoadXml(strXml);

                StringWriter sw = new StringWriter();
                using (System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(sw))
                {
                    writer.Indentation = 4;  // the Indentation
                    writer.Formatting = System.Xml.Formatting.Indented;
                    doc.WriteContentTo(writer);
                    writer.Close();
                }
                result = sw.ToString();
            }
            catch (Exception e)
            {
                result = e.Message + "---异常";
            }

            return result;
        }
    }

 

XML序列化方法

标签:orm   XML   本地   load   creat   保存   try   serialize   读取   

原文地址:https://www.cnblogs.com/jiangqw/p/12122745.html

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