SerializationUtility
public static T LoadFromXml<T>(string fileName)
{
FileStream fs = null;
try
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
return (T)serializer.Deserialize(fs);
}
finally
{
if (fs != null)
{
fs.Close();
}
}
} public static void SaveToXml<T>(string fileName, T data)
{
FileStream fs = null;
try
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
serializer.Serialize(fs, data);
}
finally
{
if (fs != null)
{
fs.Close();
}
}
} public static string XmlSerialize<T>(T serialObject)
{
StringBuilder sb = new StringBuilder();
XmlSerializer ser = new XmlSerializer(typeof(T));
using (TextWriter writer = new StringWriter(sb))
{
ser.Serialize(writer, serialObject);
return writer.ToString();
}
} public static string XmlSerialize(object serialObject)
{
StringBuilder sb = new StringBuilder();
XmlSerializer ser = new XmlSerializer(serialObject.GetType());
using (TextWriter writer = new StringWriter(sb))
{
ser.Serialize(writer, serialObject);
return writer.ToString();
}
} public static T XmlDeserialize<T>(string str)
{
XmlSerializer mySerializer = new XmlSerializer(typeof(T));
using (TextReader reader = new StringReader(str))
{
return (T)mySerializer.Deserialize(reader);
}
} public static object XmlDeserialize(string str, Type type)
{
XmlSerializer mySerializer = new XmlSerializer(type);
using (TextReader reader = new StringReader(str))
{
return mySerializer.Deserialize(reader);
}
} public static string BinarySerialize<T>(T serialObject, Encoding encoding)
{
string result = String.Empty; using (MemoryStream ms = new MemoryStream())
{
try
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, serialObject);
ms.Position = ;
result = encoding.GetString(ms.ToArray());
}
catch (Exception)
{
throw;
}
}
return result;
} public static string BinarySerialize<T>(T serialObject)
{
return BinarySerialize<T>(serialObject, Encoding.Default);
} public static string BinarySerialize(object serialObject, Encoding encoding)
{
string result = String.Empty; using (MemoryStream ms = new MemoryStream())
{
try
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, serialObject);
ms.Position = ;
result = encoding.GetString(ms.ToArray());
}
catch (Exception)
{
throw;
}
}
return result;
} public static string BinarySerialize(object serialObject)
{
return BinarySerialize(serialObject, Encoding.Default);
} public static T BinaryDeserialize<T>(string str, Encoding encoding)
{
T result = default(T);
using (MemoryStream ms = new MemoryStream())
{
try
{
BinaryFormatter formatter = new BinaryFormatter();
byte[] bytes = encoding.GetBytes(str);
ms.Write(bytes, , bytes.Length);
ms.Position = ;
result = (T)formatter.Deserialize(ms); }
catch (Exception)
{
throw;
}
}
return result;
} public static T BinaryDeserialize<T>(string str)
{
return BinaryDeserialize<T>(str, Encoding.Default);
} public static object BinaryDeserialize(string str)
{
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
byte[] bytes = Encoding.Default.GetBytes(str);
ms.Write(bytes, , bytes.Length);
ms.Position = ;
return formatter.Deserialize(ms);
}
}
}
public static string SerializeToXml(object value)
{
if (value == null)
return "";
string result = string.Empty;
if (value is string)
{
return value.ToString();
} try
{
StringBuilder sb = new StringBuilder();
XmlSerializer ser = new XmlSerializer(value.GetType());
using (TextWriter writer = new StringWriter(sb))
{
ser.Serialize(writer, value);
result = writer.ToString();
}
}
catch
{
} return result;
}
SerializationUtility的更多相关文章
- Thrift搭建分布式微服务(四)
第一篇 <连接配置> 第二篇 <连接池> 第三篇 <标准通信> 第四篇 快速暴露接口 之前的文章,我们介绍了如何使用连接池管理Thrift节点,以及使用Thri ...
- Enterprise Library 5.0 参考源码索引
http://www.projky.com/entlib/5.0/Microsoft/Practices/EnterpriseLibrary/Caching/BackgroundScheduler.c ...
- Enterprise Library 4.1 参考源码索引
http://www.projky.com/entlib/4.1/Microsoft/Practices/EnterpriseLibrary/AppSettings/Configuration/Des ...
- Enterprise Library 3.1 参考源码索引
http://www.projky.com/entlib/3.1/Microsoft/Practices/EnterpriseLibrary/AppSettings/Configuration/Des ...
- Enterprise Library 2.0 参考源码索引
http://www.projky.com/entlib/2.0/Microsoft/Practices/EnterpriseLibrary/Caching/BackgroundScheduler.c ...
- Enterprise Library 1.1 参考源码索引
http://www.projky.com/entlib/1.1/Microsoft/Practices/EnterpriseLibrary/Caching/BackgroundScheduler.c ...
随机推荐
- android通话时第二通电话呼叫等待提示音音量大小
callnotifier.java public void run() { ...... switch (mToneId) { case TO ...
- 用C++为nodejs 写组件,提高node处理效率
昨天研究了下如何用C++和node交互,在node的程序中,如果有大数据量的计算,处理起来比较慢,可以用C++来处理,然后通过回调(callback的形式),返回给node. 首先,先来看看node ...
- Initialize the Storage Emulator by Using the Command-Line Tool
http://msdn.microsoft.com/en-us/library/azure/gg433132.aspx To initialize the storage emulator Click ...
- Top 10 Universities for Artificial Intelligence
1. Massachusetts Institute of Technology, Cambridge, MA Massachusetts Institute of Technology is a p ...
- ArcGIS Server API for JavaScript调用错误:已阻止跨源请求:同源策略禁止读取位于......
已阻止跨源请求:同源策略禁止读取位于 http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapSe ...
- 文件上传限制大小 dotnet/C#
private void Button1_Click(object sender, System.EventArgs e) { if (File1.PostedFile != null) { //上传 ...
- ArcGIS与SuperMap的使用比较(1)
用了超过6年的超图产品了,因此对超图的很多特性比较熟悉,去年开始接触ARCGIS,并用来研发了一些新产品,因此对于两个GIS平台有些感受,记录如下: 比较版本:ARCGIS10.1与SuperMap ...
- 气球或者泡泡向上飘动 jQuery插件
圣诞.元旦要来了,公司以往基本每个月至少要搞一两款手机小游戏来宣传产品,这次也不例外!! 之前做过,按压柚子.许愿.吃柚子等等小游戏,这次是做个那种 气球向上飘动,戳破气球,随机获取奖品.如下图: 手 ...
- PL/SQL Developer去掉启动时自动弹出的Logon弹出框方法
以前用PL/SQL Developer 7.0版本,最近升级到PL/SQL Developer 11.0版本,但每次启动PL/SQL Developer都会自动弹出Logon窗口,并且选中其中的登录历 ...
- [Z]The Boost C++ Libraries
看起来是个学习boost非常不错的材料,应该是boost的官方教程之类: http://theboostcpplibraries.com/