zoukankan      html  css  js  c++  java
  • 序列化和反序列化

        /// <summary>
            /// 读取XML文件,反序列化为对象
            /// </summary>
            /// <typeparam name="T">对象类型</typeparam>
            /// <param name="t">对象</param>
            /// <param name="fileName">xml文件名</param>
            /// <returns>反序列化的对象</returns>
            public static T ReadXMLFileToType<T>(T t, string xmlFileName)
            {
                XmlSerializer xs = new XmlSerializer(t.GetType());
                XmlTextReader reader = new XmlTextReader(xmlFileName);
                reader.Normalization = false;
                t = (T)xs.Deserialize(reader);
                reader.Close();
                return t;
            }

            /// <summary>
            /// 序列化对象,保存为XML文件,前缀为空
            /// </summary>
            /// <param name="t">对象类型</param>
            /// <param name="s">对象序列化后的Xml文件</param>
            public static void SaveTypeToXmlFile<T>(T t, string xmlFile)
            {
                XmlSerializer xs = new XmlSerializer(t.GetType());
                TextWriter writer = new StreamWriter(xmlFile);
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add(String.Empty, String.Empty);
                xs.Serialize(writer, t, ns);
                writer.Flush();
                writer.Close();
            }
        }

  • 相关阅读:
    Kubernetes 认证(证书)过期怎么办
    JavaScript 全屏显示窗口
    IE6下很无语的问题,不知为何
    项目开发-让设计模式成为一种心智(转)
    CSS中Float概念相关文章采撷
    随记浏览器兼容性
    常用正则表达式
    ASP.NET 调用Delphi DLL问题
    ASP.NET调用DELPHI DLL
    转:Oracle 排序中常用的NULL值处理方法
  • 原文地址:https://www.cnblogs.com/Veakey/p/2674612.html
Copyright © 2011-2022 走看看