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);}}

转载于:https://www.cnblogs.com/marshhu/p/6780127.html

Xml、Json序列化相关推荐

  1. C#里XML(JSON)序列化时,自动隐藏值为Null的成员的输出

    原文:C#里XML(JSON)序列化时,自动隐藏值为Null的成员的输出 从StackOverflow里找到的答案.发现对最新的Newtownsoft的JSON序列化也同样适用. https://st ...

  2. xml 和 json 序列化忽略字段

    xml 和 json 序列化忽略字段: @JsonIgnore@XmlTransient 转载于:https://www.cnblogs.com/xiluhua/p/9192577.html

  3. wp8.1 Study11:APP里文件读写和使用XML和Json序列化

    一.文件读写 1.基本操作(使用FileIO API) 这个方法在上一个stduy已经学过,那么贴出来复习下,代码如下: private async void writeTextToLocalStor ...

  4. 二进制序列化器、XML序列化器、Json序列化器

    二进制序列化器.XML序列化器.Json序列化器 序列化是将对象的状态信息转换未可以存储或传输的形式的过程.在序列化期间,对象将其当前状态写入到临时或持久性存储区.以后,可以通过从存储区读取或反序列化 ...

  5. DotNet的JSON序列化与反序列化

    JSON(JavaScript Object Notation)JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式.在现在的通信中,较多的采用JSON数据格式,JSON有 ...

  6. C#中JSON序列化和反序列化

    有一段时间没有到博客园写技术博客了,不过每天逛逛博客园中大牛的博客还是有的,学无止境-- 最近在写些调用他人接口的程序,用到了大量的JSON.XML序列化和反序列化,今天就来总结下json的序列化和反 ...

  7. json 序列化 java_Java JSON序列化杂谈

    在C#中序列化是比较容易的,如果不用第三方的类库,那么C#提供的两个对象也够我们使用,一般的操作是定义一个不含有方法的类,这个类里面定义了要序列化的对象的属性,因为我C#用的比较多,这里直接给出泛型, ...

  8. JAVA——json序列化错误[hibernateLazyInitializer,handler,fieldHandler]解决方案

    问题描述 (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain ...

  9. 简单了解各种序列化技术-JSON序列化框架

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,相对于XML来说,JSON的字节流更小,而且可读性也非常好.现在JSON数据格式在企业运用是最普遍的 JSO ...

最新文章

  1. linux如何关闭 lvm管理,Linux之LVM管理
  2. bootstrap表格某一列值相同时_Bootstrap-table实现动态合并相同行(表格同名合并)
  3. 数论初步——同余与模算术
  4. 07.十分钟学会tomcat数据源
  5. linux源码安装必备条件,玩转Linux必备的金钥匙之源码安装mysql
  6. Neo4j:Cypher –避免热切
  7. Python中xlrd模块解析
  8. 微软云打印将直接与 OneDrive 集成;全球 90 多家组织敦促苹果放弃引入”儿童安全”功能计划……...
  9. java 抛出异常效率_Java异常处理机制
  10. 网络编程(一)基础知识
  11. 【Docker】docker bash: sudo: command not found
  12. 中国内脏痛行业市场供需与战略研究报告
  13. 微软自带的防反编译工具dotfuscator.exe的使用
  14. 拓端tecdat|R语言公交地铁路线进出站数据挖掘网络图可视化
  15. 在Windows下搭建RocketMQ
  16. 第25版 OpenStack Yoga 已发布:稳定性与创新并重
  17. VM 虚拟机 分辨率问题
  18. 大数据去重bitmap以及布隆过滤器
  19. StarUML license key
  20. 集合(Collection)的详细笔记

热门文章

  1. Linux下查看文件占用空间大小的du 和df 命令
  2. Android P 调用隐藏API限制原理
  3. 深入解读EOS源代码之——区块链内核
  4. 利用FRIDA攻击Android应用程序(一)
  5. JZOJ 5186. 【NOIP2017提高组模拟6.30】tty's home
  6. ACM竞赛学习整理--Gauss求解POJ1166
  7. websocket 更新点位 浏览器卡顿_我们来看看Swoole是如何实现WebSocket服务器及客户端的...
  8. Tex 表格注释实例
  9. 粒子群(PSO)算法简介
  10. 数据挖掘讲座:我所知道的一点Data Mining