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 = ;
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序列化的更多相关文章
- windows phone8.1:Xml,Json序列化和反序列化
原文:windows phone8.1:Xml,Json序列化和反序列化 小梦本例主要实现以下四点内容: 将Car对象序列化为xml 将Car对象序列化为Json 将xml反序列化为Car对象 将js ...
- C#里XML(JSON)序列化时,自动隐藏值为Null的成员的输出
从StackOverflow里找到的答案.发现对最新的Newtownsoft的JSON序列化也同样适用. https://stackoverflow.com/questions/5818513/xml ...
- Xml,Json,Hessian,Protocol Buffers序列化对比
简介 这篇博客主要对Xml,Json,Hessian,Protocol Buffers的序列化和反序列化性能进行对比,Xml和Json的基本概念就不说了. Hessian:Hessian是一个轻量级的 ...
- wp8.1 Study11:APP里文件读写和使用XML和Json序列化
一.文件读写 1.基本操作(使用FileIO API) 这个方法在上一个stduy已经学过,那么贴出来复习下,代码如下: private async void writeTextToLocalStor ...
- WebApi2官网学习记录---JSON与XML的序列化
JSON序列化: WebAPI的默认序列库使用的是Json.NET,可以在Globally中配置使用DataContractJsonSerializer 进行序列化 protected void Ap ...
- xml 和 json 序列化忽略字段
xml 和 json 序列化忽略字段: @JsonIgnore @XmlTransient
- xml的序列化与反序列化求一个好用的东西,类似,newtonsoft.net转json的东西。xml里面的结构和数据库不一致..................
xml的序列化与反序列化求一个好用的东西,类似,newtonsoft.net转json的东西.xml里面的结构和数据库不一致..................
- C# 序列化详解,xml序列化,json序列化对比
本文讲讲一些纯技术的东西.并且讲讲一些原理性的东西,和一般的百度的文章不一致,如果你对序列化不清楚,绝对可以很有收获. 技术支持QQ群(主要面向工业软件及HSL组件的):592132877 (组件的 ...
- json序列化.xml序列化.图片转base64.base64转图片.生成缩略图.IEnumerable<TResult> Select<TSource, TResult>做数据转换的五种方式
JSON序列化 /// <summary> /// JSON序列化 /// </summary> public static class SPDBJsonConvert { ...
随机推荐
- Linux 操作基础(一) -- Shell 命令格式和元字符
1 命令格式 cmd [-选项] [参数] 说明: • 最简单的Shell命令只有命令名,复杂的Shell命令可以有多个选项和参数 • 参数是文件也可以是目录,有些命令必须使用多个操作对象 • 并非所 ...
- iOS:UISplitViewController的创建
UISplitViewController是iPad特有的系统方法,主要效果就是呈现iPad的经典切割界面 代码创建实例: - (BOOL)application:(UIApplication *)a ...
- 2、java注释、标识符、数据类型、类型转换
一.三种注释:单行注释.多行注释.文档注释(只能在类前或者方法前,@author作者) 二.java使用的编码为unicode码[0-65535] 包含ASCII码,在0-255中 ASCII码( ...
- python之路-------字符串与正則表達式
1.1.#####去掉字符串中的转义符string.strip() print "hello\tworld\n" >>> word="\thello w ...
- 【翻译自mos文章】开启dblink的 oracle net trace/tracing --对dblink进行跟踪的方法
开启dblink的 oracle net trace/tracing --对dblink进行跟踪的方法. 參考原文: DBLINK: How to Enable Oracle Net Tracing ...
- Linux下安装Mysql(RPM安装)
一.去官网下载本次安装须要的mysql版本号.我们须要安装的是5.1版本号的且centos系统是64位的.所下面载的是MySQL-community-5.1.73-1.rhel5.x86_64.rpm ...
- 【RQNOJ】460 诺诺的队列
[题目大意] 求全部数对(i,j)满足随意a[k]<=a[i]且a[k]<=a[j]. 形象地说,就是有一群人站成一列.每一个人有一定的身高,然后问有多少对人能够互相看得到. 把数对(i, ...
- cocos2d-x中的二段构造模式
学习cocos2d-x的过程中,会发现很多对象都通过一个静态函数create来创建.比方以下的一个样例 #define CREATE_FUNC (__TYPE__) \ static __TYPE__ ...
- MongoDB Shell (mongo)
https://docs.mongodb.com/getting-started/shell/client/ The mongo shell is an interactive JavaScript ...
- spark 卡在spark context,运行出现spark Exception encountered while connecting to the server : javax.security.sasl.SaslException
原因: 使用root用户运行spark代码 解决方法:使用非管理员账户运行spark即可 [userone@localhost bin]$ ./add-user.sh What type of use ...