xml对象序列化

    public static class XSerializer
{ /// <summary>
/// 将对象序列化为xml字符串
/// </summary>
/// <typeparam name="T">类型<peparam>
/// <param name="t">对象</param>
public static string ObjectToXml<T>(T t) where T : class
{
XmlSerializer formatter = new XmlSerializer(typeof(T));
using (MemoryStream stream = new MemoryStream())
{
formatter.Serialize(stream, t);
string result = Encoding.UTF8.GetString(stream.ToArray());
return result;
}
} /// <summary>
/// 字符串转换为对象
/// </summary>
/// <typeparam name="T"><peparam>
/// <param name="t"></param>
/// <param name="xml"></param>
/// <returns></returns>
public static T XmlToObject<T>(string xml) where T : class
{
XmlSerializer formatter = new XmlSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
{
T result = formatter.Deserialize(ms) as T;
return result;
}
} }

  

上一篇:python – 压缩文件在h5py中更大


下一篇:转:eclipse技巧之快速生成Override函数