• XML序列化反序列化—常用类


    public class XMLSerializer
        {
            #region (public) xml序列化
            /// <summary>
            /// xml序列化
            /// </summary>
            /// <param name="obj">obj类</param>
            /// <returns>string字符串</returns>
            public static string XMLSerialize(object obj)
            {
                XmlSerializer xs = new XmlSerializer(obj.GetType());
                StringBuilder strBuidler = new StringBuilder();
                XmlWriterSettings setting = new XmlWriterSettings();
                setting.OmitXmlDeclaration = true;//去掉xml版本声明
                System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(strBuidler, setting);
                XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
                xmlns.Add(string.Empty, string.Empty);
                xs.Serialize(xw, obj, xmlns);
                xw.Close();
                return strBuidler.ToString();
            }
            #endregion

            #region (public) xml序列化
            /// <summary>
            /// xml序列化
            /// </summary>
            /// <param name="obj">obj类</param>
            /// <returns>string字符串</returns>
            public static string ConvertToString(object objectToConvert)
            {
                string xml = null;
                if (objectToConvert == null)
                    return xml;

                Type t = objectToConvert.GetType();

                XmlSerializer ser = new XmlSerializer(t);
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
                {
                    ser.Serialize(writer, objectToConvert, ns);
                    xml = writer.ToString();
                    writer.Close();
                }
                return xml;
            }
            #endregion

            #region (public) xml反序列化
            /// <summary>
            /// xml反序列化
            /// </summary>
            /// <param name="s">字符串string</param>
            /// <param name="type">obj type</param>
            /// <returns>obj</returns>
            public static object XMLDeserialize(string s, Type type)
            {


                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(s);
                s = xmlDoc.DocumentElement.OuterXml;
                XmlSerializer xs = new XmlSerializer(type);
                Stream stream = new System.IO.MemoryStream(System.Text.ASCIIEncoding.UTF8.GetBytes(s));
                object obj = xs.Deserialize(stream);
                stream.Close();
                return obj;
            }
            #endregion

            #region (public) xml反序列化

            /// <summary>
            /// xml to dataset
            /// </summary>
            /// <param name="xmlData">xml字符串</param>
            /// <returns>dataset</returns>
            public static DataSet XML2DataSet(string xmlData)
            {
                StringReader stream = null;
                XmlTextReader reader = null;
                try
                {
                    DataSet xmlDS = new DataSet();
                    stream = new StringReader(xmlData);
                    reader = new XmlTextReader(stream);
                    xmlDS.ReadXml(reader);
                    return xmlDS;
                }
                catch (Exception ex)
                {
                    string strTest = ex.Message;
                    return null;
                }
                finally
                {
                    if (reader != null)
                        reader.Close();
                }
            }
            #endregion

            #region (public) XML反序列化
            /// <summary>
            /// 读取文件转化为对象
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="objectType">对象类型</param>
            /// <returns>对象</returns>
            public static object ConvertFileToObject(string path, Type objectType)
            {
                object convertedObject = null;

                if (path != null && path.Length > 0)
                {
                    using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                    {
                        XmlSerializer ser = new XmlSerializer(objectType);
                        convertedObject = ser.Deserialize(fs);
                        fs.Close();
                    }
                }
                return convertedObject;
            }
            #endregion

            #region (public) xml 反序列化
            /// <summary>
            /// 将xml字符串转换为对应的实体
            /// </summary>
            /// <typeparam name="T">实体类型</typeparam>
            /// <param name="xmlDocument">xml字符串</param>
            /// <returns>实体类型对象</returns>
            public static T ConvertXmlToObject<T>(string xml) where T : class, new()
            {
                if (string.IsNullOrEmpty(xml)) return new T();
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                T resultObject;
                using (TextReader reader = new StringReader(xml))
                {
                    resultObject = (T)serializer.Deserialize(reader);
                }
                return resultObject;
            }

            #endregion

            #region(public) 对象保存为xml
            /// <summary>
            ///  把对象序列化为XML 并保存为文件
            /// </summary>
            /// <param name="objectToConvert">对象</param>
            /// <param name="path">路径</param>
            public static void SaveAsXML(object objectToConvert, string path)
            {
                if (objectToConvert != null)
                {
                    Type t = objectToConvert.GetType();
                    XmlSerializer ser = new XmlSerializer(t);
                    using (StreamWriter writer = new StreamWriter(path))
                    {
                        ser.Serialize(writer, objectToConvert);
                        writer.Close();
                    }
                }
            }
            #endregion
        }

  • 相关阅读:
    [每日一讲] Python系列:浅拷贝与深拷贝
    [每日一讲] Python系列:变量、内存管理与传递
    [每日一讲] Python系列:字典
    [每日一讲] Python系列:列表与元组
    [已开源/文章教程]独立开发 一个社交 APP 的源码/架构分享 (已上架)
    从选择到上传,可能是最贴心的高仿朋友圈编辑了
    完整的社交app源码android+laravel
    基于Laravel+Swoole开发智能家居后端
    APP架子迁移指南(三)
    Laravel如何优雅的使用Swoole
  • 原文地址:https://www.cnblogs.com/tonglei/p/4422871.html
Copyright © 2020-2023  润新知