最近在跟MySql打交道,PHP的程序是将实体中的数据整个序列化后存入数据库中,形成的格式是
a:2:{i:1;i:1;s:5:"value";a:2:{i:1;i:1;s:5:"value";N;}}
这让我思考,我在.net程序中能否读取出来呢,好像.net也没有这样格式的序列化方法,由于PHP的数据是没有类型的。
这可如何办捏....????
将序列化的方法都列一遍
#region XML序列化
/// <summary>
/// 将对象序列化为Xml格式字符串
/// </summary>
/// <param name="obj">对象</param>
/// <returns></returns>
public static string SerializeToXml(object obj)
{
StringWriter sw = null;
try
{
XmlSerializer xmlser = new XmlSerializer(obj.GetType());
sw = new StringWriter();
xmlser.Serialize(sw, obj);
return sw.ToString();
}
catch (Exception ex)
{
throw new Exception("序列化失败", ex);
}
finally
{
sw.Close();
sw.Dispose();
}
}
#endregion
#region XML反序列化
/// <summary>
/// 将Xml格式字符串反序列化为对象
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="str">反序列的字符串</param>
/// <returns></returns>
public static T DeserializeToXml<T>(string str)
{
StringReader sr = null;
try
{
XmlSerializer xmlser = new XmlSerializer(typeof(T));
sr = new StringReader(str);
return (T)xmlser.Deserialize(sr);
}
catch (Exception ex)
{
throw new Exception("反序列化失败", ex);
}
finally
{
sr.Close();
sr.Dispose();
}
}
#endregion
#region 二进制序列化
/// <summary>
/// 将对象序列化为Base64编码字符串
/// </summary>
/// <param name="obj">对象</param>
/// <returns></returns>
public static string SerializeToBase64(object obj)
{
MemoryStream ms = null;
try
{
BinaryFormatter format = new BinaryFormatter();
ms = new MemoryStream();
byte[] bytes = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(bytes, 0, bytes.Length);
return Convert.ToBase64String(bytes);
}
catch (Exception ex)
{
throw new Exception("序列化失败!", ex);
}
finally
{
ms.Close();
ms.Dispose();
}
}
#endregion
#region 二进制反序列化
/// <summary>
/// 将Base64编码的字符串反序列化为对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="str"></param>
/// <returns></returns>
public static T DeserializeToBase64<T>(string str)
{
MemoryStream ms = null;
try
{
BinaryFormatter format = new BinaryFormatter();
byte[] bytes = Convert.FromBase64String(str);
ms = new MemoryStream();
ms.Write(bytes, 0, bytes.Length);
ms.Seek(0, SeekOrigin.Begin);
return (T)format.Deserialize(ms);
}
catch (Exception ex)
{
throw new Exception("反序列化失败!", ex);
}
finally
{
ms.Close();
ms.Dispose();
}
}
#endregion