Demo
public class XmlSerializeUtils
{
private static Dictionary<int, XmlSerializer> serializer_dict = new Dictionary<int, XmlSerializer>();
/// <summary>
/// 获取指定类型的 XmlSerializer
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static XmlSerializer GetSerializer(Type t)
{
int type_hash = t.GetHashCode();
if (!serializer_dict.ContainsKey(type_hash))
serializer_dict.Add(type_hash, new XmlSerializer(t));
return serializer_dict[type_hash];
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="filename">文件路径</param>
/// <returns></returns>
public static T Load<T>(string filename)
{
Type type=typeof(T);
FileStream fs = null;
try
{
// open the stream...
fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(type);
return (T)serializer.Deserialize(fs);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null)
fs.Close();
}
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="obj">对象</param>
/// <param name="filename">文件路径</param>
public static bool Save(object obj, string filename)
{
bool success = false;
FileStream fs = null;
// serialize it...
try
{
fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(fs, obj);
success = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null)
fs.Close();
}
return success;
}
/// <summary>
/// xml序列化成字符串
/// </summary>
/// <param name="obj">对象</param>
/// <returns>xml字符串</returns>
public static string Serialize(object obj)
{
string returnStr = "";
XmlSerializer serializer = GetSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
XmlTextWriter xtw = null;
StreamReader sr = null;
try
{
xtw = new System.Xml.XmlTextWriter(ms, Encoding.UTF8);
xtw.Formatting = System.Xml.Formatting.Indented;
serializer.Serialize(xtw, obj);
ms.Seek(0, SeekOrigin.Begin);
sr = new StreamReader(ms);
returnStr = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (xtw != null)
xtw.Close();
if (sr != null)
sr.Close();
ms.Close();
}
return returnStr;
}
/// <summary>
/// 从字符串反序列化为对象
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static T DeSerialize<T>(string s)
{
Type type = typeof(T);
byte[] b = System.Text.Encoding.UTF8.GetBytes(s);
try
{
XmlSerializer serializer = GetSerializer(type);
return (T)serializer.Deserialize(new MemoryStream(b));
}
catch (Exception ex)
{
throw ex;
}
}
}
{
private static Dictionary<int, XmlSerializer> serializer_dict = new Dictionary<int, XmlSerializer>();
/// <summary>
/// 获取指定类型的 XmlSerializer
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static XmlSerializer GetSerializer(Type t)
{
int type_hash = t.GetHashCode();
if (!serializer_dict.ContainsKey(type_hash))
serializer_dict.Add(type_hash, new XmlSerializer(t));
return serializer_dict[type_hash];
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="filename">文件路径</param>
/// <returns></returns>
public static T Load<T>(string filename)
{
Type type=typeof(T);
FileStream fs = null;
try
{
// open the stream...
fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(type);
return (T)serializer.Deserialize(fs);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null)
fs.Close();
}
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="obj">对象</param>
/// <param name="filename">文件路径</param>
public static bool Save(object obj, string filename)
{
bool success = false;
FileStream fs = null;
// serialize it...
try
{
fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(fs, obj);
success = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null)
fs.Close();
}
return success;
}
/// <summary>
/// xml序列化成字符串
/// </summary>
/// <param name="obj">对象</param>
/// <returns>xml字符串</returns>
public static string Serialize(object obj)
{
string returnStr = "";
XmlSerializer serializer = GetSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
XmlTextWriter xtw = null;
StreamReader sr = null;
try
{
xtw = new System.Xml.XmlTextWriter(ms, Encoding.UTF8);
xtw.Formatting = System.Xml.Formatting.Indented;
serializer.Serialize(xtw, obj);
ms.Seek(0, SeekOrigin.Begin);
sr = new StreamReader(ms);
returnStr = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (xtw != null)
xtw.Close();
if (sr != null)
sr.Close();
ms.Close();
}
return returnStr;
}
/// <summary>
/// 从字符串反序列化为对象
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static T DeSerialize<T>(string s)
{
Type type = typeof(T);
byte[] b = System.Text.Encoding.UTF8.GetBytes(s);
try
{
XmlSerializer serializer = GetSerializer(type);
return (T)serializer.Deserialize(new MemoryStream(b));
}
catch (Exception ex)
{
throw ex;
}
}
}