zoukankan      html  css  js  c++  java
  • Xml、Json序列化

    Xml序列化:

      public class XmlHelper 
        {
            private static string XmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings["XmlPath"]);
    
            public static T FileToObject<T>(string fileName) where T : new()
            {
                string fullName = Path.Combine(XmlPath, fileName);
                if (File.Exists(fullName))
                {
                    using (Stream fStream = new FileStream(fullName, FileMode.Open, FileAccess.ReadWrite))
                    {
                        XmlSerializer xmlFormat = new XmlSerializer(typeof(T));
                        return (T)xmlFormat.Deserialize(fStream);
                    }
                }
                else
                {
                    return default(T);
                }
                
            }
    
            public static void ObjectToFile<T>(T obj, string fileName) where T : new()
            {
                string fullName = Path.Combine(XmlPath, fileName);
                string fullPath = Path.GetDirectoryName(fullName);
                if (!Directory.Exists(fullPath))
                {
                    Directory.CreateDirectory(fullPath);
                }
                using (Stream fStream = new FileStream(fullName, FileMode.Create, FileAccess.ReadWrite))
                {
                    XmlSerializer xmlFormat = new XmlSerializer(typeof(T));
                    xmlFormat.Serialize(fStream, obj);
                }
            }
    
            public static string ObjectToString<T>(T obj) where T : new()
            {
                XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
                Stream stream = new MemoryStream();
                xmlSerializer.Serialize(stream, obj);
                stream.Position = 0;
                StreamReader reader = new StreamReader(stream);
                return reader.ReadToEnd();
            }
    
            public static  T StringToObject<T>(string content) where T : new()
            {
                using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(content)))
                {
                    XmlSerializer xmlFormat = new XmlSerializer(typeof(T));
                    return (T)xmlFormat.Deserialize(stream);
                }
            }
        }

    Json序列化:

    /// <summary>
        /// Json序列化器
        /// </summary>
        public class JsonHelper 
        {
            private static string JsonPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings["JsonPath"]);
    
            public static T FileToObject<T>(string fileName) where T : new()
            {
                string fullName = Path.Combine(JsonPath, fileName);
                if (File.Exists(fullName))
                {
                    return StringToObject<T>(File.ReadAllText(fullName, Encoding.Default));
                }
                else
                {
                    return default(T);
                }
            }
    
            public static void ObjectToFile<T>(T obj, string fileName) where T : new()
            {
                string fullName = Path.Combine(JsonPath, fileName);
                string fullPath = Path.GetDirectoryName(fullName);
                if (!Directory.Exists(fullPath))
                {
                    Directory.CreateDirectory(fullPath);
                }
                using (FileStream fileStream = File.Create(fullName))
                {
                    string text = JsonConvert.SerializeObject(obj);
                    byte[] bytes = Encoding.Default.GetBytes(text);
                    fileStream.Write(bytes, 0, bytes.Length);
                }
            }
    
            public static string ObjectToString<T>(T obj) where T : new()
            {
                return JsonConvert.SerializeObject(obj);
            }
    
            public static T StringToObject<T>(string content) where T : new()
            {
                return JsonConvert.DeserializeObject<T>(content);
            }
        }
  • 相关阅读:
    PyCharm安装及其使用
    web端自动化——Selenium3+python自动化(3.7版本)-chrome67环境搭建
    Unittest单元测试框架
    selenium IDE下载安装(For Chrome and firefox)
    视频上传测试点
    web端自动化——自动化测试准备工作
    selenium3+Python3+sublime text3自动化登录
    Sublime Text3安装及常用插件安装
    web端自动化——selenium3用法详解
    Selenium2+python自动化2.7-火狐44版本环境搭建(转)
  • 原文地址:https://www.cnblogs.com/marshhu/p/6780127.html
Copyright © 2011-2022 走看看