JSON

Xml、Json序列化

字号+ 作者:H5之家 来源:H5之家 2017-05-04 10:00 我要评论( )

Xml序列化: Json序列化: ...

标签:span   adt   mod   type   tor   void   form   director   tty   

Xml序列化:

public class XmlHelper { XmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings[]); 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); } } 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); } } 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序列化:

Json序列化器 JsonHelper { JsonPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings[]); 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); } } 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); } } 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序列化

标签:span   adt   mod   type   tor   void   form   director   tty   

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 关注站长和分享互联网创业及VIP资源干货教程的新媒体营销博客

    关注站长和分享互联网创业及VIP资源干货教程的新媒体营销博客

    2017-05-04 08:08

  • Android快速开发教程第一季

    Android快速开发教程第一季

    2017-03-29 15:00

  • PWA(Progressive Web App)入门系列:(一)PWA简介

    PWA(Progressive Web App)入门系列:(一)PWA简介

    2017-03-23 09:01

  • 如何让ASP.NET WEB API 默认回应JSON 格式

    如何让ASP.NET WEB API 默认回应JSON 格式

    2017-03-21 14:00

网友点评
c