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的更多相关文章

  1. Thrift搭建分布式微服务(四)

      第一篇 <连接配置> 第二篇 <连接池> 第三篇 <标准通信> 第四篇 快速暴露接口 之前的文章,我们介绍了如何使用连接池管理Thrift节点,以及使用Thri ...

  2. Enterprise Library 5.0 参考源码索引

    http://www.projky.com/entlib/5.0/Microsoft/Practices/EnterpriseLibrary/Caching/BackgroundScheduler.c ...

  3. Enterprise Library 4.1 参考源码索引

    http://www.projky.com/entlib/4.1/Microsoft/Practices/EnterpriseLibrary/AppSettings/Configuration/Des ...

  4. Enterprise Library 3.1 参考源码索引

    http://www.projky.com/entlib/3.1/Microsoft/Practices/EnterpriseLibrary/AppSettings/Configuration/Des ...

  5. Enterprise Library 2.0 参考源码索引

    http://www.projky.com/entlib/2.0/Microsoft/Practices/EnterpriseLibrary/Caching/BackgroundScheduler.c ...

  6. Enterprise Library 1.1 参考源码索引

    http://www.projky.com/entlib/1.1/Microsoft/Practices/EnterpriseLibrary/Caching/BackgroundScheduler.c ...

随机推荐

  1. Windows 10开启默认网络驱动器访问

    在Windows 10的系统策略中,驱动器盘符的网络访问是默认关闭的,用管理员权限打开注册表,找到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Curre ...

  2. Codeforces Round #384 (Div. 2) C. Vladik and fractions 构造题

    C. Vladik and fractions 题目链接 http://codeforces.com/contest/743/problem/C 题面 Vladik and Chloe decided ...

  3. awk语法

    awk是一个非常棒的数字处理工具.相比于sed常常作用于一整行的处理,awk则比较倾向于将一行分为数个“字段”来处理.运行效率高,而且代码简单,对格式化的文本处理能力超强.先来一个例子: 文件a,统计 ...

  4. wordpress自动批量定时发布插件 DX-auto-publish

    DX-auto-publish是一款wordpress自动发布插件,方便实用. 该wordpress插件的主要功能如下: 1.能够自动批量定时发布wordpress站点的草稿文章,无需每篇文章都手动设 ...

  5. TextBox 文本框水印文字

    #region TextBox 文本框水印文字 /// <summary> /// 基于.NET 2.0的TextBox工具类 /// </summary> public st ...

  6. Android NDK 同时编译多个Module

    LOCAL_PATH := $(call my-dir) ## ## NDK 支持同时编译多个Module: ## 在配置的时候,每个Module需要 以 include $(CLEAR_VARS)开 ...

  7. Generating a new ASP.NET session in the current HTTPContext

    void regenerateId() { System.Web.SessionState.SessionIDManager manager = new System.Web.SessionState ...

  8. SAP ECC MM 配置文档

    SAP ECC 6.0 Configuration Document Materials Management (MM) Table of Content TOC \o \h \z 1. Genera ...

  9. 关于 Visual Studio 调试 Global 的一点总结

    在开发 MVC 的项目中遇到了些问题,想通过调戏查看找问题的原因,发现无法调试 Global 中的 Application_Start 方法,在网上找遍了也没有相应的解决办法,在经过了很多次尝试之后仍 ...

  10. CORS(跨来源资源共享协议) 与 http 302状态

    昨天遇到的问题 使用ajax请求一个支持CORS的跨域页面(A),此页面返回302状态并且重新定向到页面(B).此时ajax停止不前,并且触发 ajax onerror 事件. 正确的相应应该是:aj ...