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

Xml、Json序列化的更多相关文章

  1. windows phone8.1:Xml,Json序列化和反序列化

    原文:windows phone8.1:Xml,Json序列化和反序列化 小梦本例主要实现以下四点内容: 将Car对象序列化为xml 将Car对象序列化为Json 将xml反序列化为Car对象 将js ...

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

    从StackOverflow里找到的答案.发现对最新的Newtownsoft的JSON序列化也同样适用. https://stackoverflow.com/questions/5818513/xml ...

  3. Xml,Json,Hessian,Protocol Buffers序列化对比

    简介 这篇博客主要对Xml,Json,Hessian,Protocol Buffers的序列化和反序列化性能进行对比,Xml和Json的基本概念就不说了. Hessian:Hessian是一个轻量级的 ...

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

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

  5. WebApi2官网学习记录---JSON与XML的序列化

    JSON序列化: WebAPI的默认序列库使用的是Json.NET,可以在Globally中配置使用DataContractJsonSerializer 进行序列化 protected void Ap ...

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

    xml 和 json 序列化忽略字段: @JsonIgnore @XmlTransient

  7. xml的序列化与反序列化求一个好用的东西,类似,newtonsoft.net转json的东西。xml里面的结构和数据库不一致..................

    xml的序列化与反序列化求一个好用的东西,类似,newtonsoft.net转json的东西.xml里面的结构和数据库不一致..................

  8. C# 序列化详解,xml序列化,json序列化对比

    本文讲讲一些纯技术的东西.并且讲讲一些原理性的东西,和一般的百度的文章不一致,如果你对序列化不清楚,绝对可以很有收获. 技术支持QQ群(主要面向工业软件及HSL组件的):592132877  (组件的 ...

  9. json序列化.xml序列化.图片转base64.base64转图片.生成缩略图.IEnumerable<TResult> Select<TSource, TResult>做数据转换的五种方式

     JSON序列化 /// <summary> /// JSON序列化 /// </summary> public static class SPDBJsonConvert { ...

随机推荐

  1. python 面向对象 类方法,静态方法,property

    property 内置装饰器函数 只在面向对象使用 把方法当初属性使用(方法不加参数) 例子: class Rectangle: def __init__(self,long,wide,color): ...

  2. 紫书 例题8-19 UVa 12265 (扫描法+单调栈)

    首先可以用扫描法处理出一个height数组, 来保存从当前行开始, 每一个格子可以向上延伸的最大长度. 这种"延伸"的问题用扫描法, 因为往往这个时候可以利用前一次的结果来更新当前 ...

  3. 【CS Round 34】Max Or Subarray

    [题目链接]:https://csacademy.com/contest/round-34/summary/ [题意] 让你找一个最短的连续子串; 使得这个子串里面所有数字or起来最大; [题解] 对 ...

  4. bzoj1296: [SCOI2009]粉刷匠(DP)

    1296: [SCOI2009]粉刷匠 题目:传送门 题解: DP新姿势:dp套dp 我们先单独处理每个串,然后再放到全局更新: f[i][k]表示当前串枚举到第i个位置,用了k次机会 F[i][j] ...

  5. 使用iOS原生sqlite3框架对sqlite数据库进行操作

    摘要: iOS中sqlite3框架可以很好的对sqlite数据库进行支持,通过面向对象的封装,可以更易于开发者使用. 使用iOS原生sqlite3框架对sqlite数据库进行操作 一.引言 sqlit ...

  6. C语言基础-第六章

    数组和字符串 1.一维数组 数组当中最简单的数据 声明: 类型说明符 数组名[常量表达式] int a[3];说明a的长度为3,那么给a赋值的语句是:a={1,2,3}; 2.多维数组 2.1 二维数 ...

  7. Spring4+SpringMVC+MyBatis登录注册详细

    项目结构: package com.mstf.controller; import org.springframework.stereotype.Controller; import org.spri ...

  8. ViewPager设置不能滚动

    设置ViewPager不能滑动 1:设置当前选中的页面 public void setCurrentItem(int item) { mPopulatePending = false; setCurr ...

  9. Spring mvc 启动 和 请求分发

    Spring mvc 启动 和 请求分发 启动加载: abstract class HttpServletBean extends HttpServlet void init() initServle ...

  10. BAPC 2014 Preliminary(第一场)

    D:Lift Problems On the ground floor (floor zero) of a large university building a number of students ...