序列化与反序列化
扩展相关类的方法:
Serialization
SiteUser
在页面中使用示例
SiteUser.XML
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
public static class Serialization
{
/// <summary>
/// 将对象序列化成字符串
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string SerializeToString( object obj) {
IFormatter formater = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formater.Serialize(stream, obj);
stream.Position = 0;
byte[] b = new byte[stream.Length];
stream.Read(b, 0, b.Length);
stream.Close();
return Convert.ToBase64String(b);
}
/// <summary>
/// 将字符串序列化为对象
/// </summary>
/// <param name="obj"></param>
/// <param name="data"></param>
/// <returns></returns>
public static object DeSerializeObject(this string data) {
byte[] b = Convert.FromBase64String(data);
IFormatter formater = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
stream.Write(b, 0, b.Length);
stream.Position = 0;
return (object)formater.Deserialize(stream);
}
/// <summary>
/// 将对象序列化到 XML
/// </summary>
/// <param name="savepath">XML文件保存的地址 </param>
/// <param name="obj"></param>
public static void SerializeToXML( this object obj,
string savepath , Type user ) {
XmlSerializer xs = new XmlSerializer(user);
Stream stream = new FileStream(savepath, FileMode.Create,
FileAccess.Write, FileShare.Write);
xs.Serialize(stream, obj);
stream.Close();
}
/// <summary>
/// 将 XML 序列化为对象
/// </summary>
/// <param name="obj"></param>
/// <param name="t"></param>
/// <param name="path"></param>
/// <returns></returns>
public static object DeSerializeFromXML(this object obj,
Type t, string path) {
XmlSerializer xs = new XmlSerializer(t);
Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
return xs.Deserialize(stream);
}
}
{
/// <summary>
/// 将对象序列化成字符串
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string SerializeToString( object obj) {
IFormatter formater = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formater.Serialize(stream, obj);
stream.Position = 0;
byte[] b = new byte[stream.Length];
stream.Read(b, 0, b.Length);
stream.Close();
return Convert.ToBase64String(b);
}
/// <summary>
/// 将字符串序列化为对象
/// </summary>
/// <param name="obj"></param>
/// <param name="data"></param>
/// <returns></returns>
public static object DeSerializeObject(this string data) {
byte[] b = Convert.FromBase64String(data);
IFormatter formater = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
stream.Write(b, 0, b.Length);
stream.Position = 0;
return (object)formater.Deserialize(stream);
}
/// <summary>
/// 将对象序列化到 XML
/// </summary>
/// <param name="savepath">XML文件保存的地址 </param>
/// <param name="obj"></param>
public static void SerializeToXML( this object obj,
string savepath , Type user ) {
XmlSerializer xs = new XmlSerializer(user);
Stream stream = new FileStream(savepath, FileMode.Create,
FileAccess.Write, FileShare.Write);
xs.Serialize(stream, obj);
stream.Close();
}
/// <summary>
/// 将 XML 序列化为对象
/// </summary>
/// <param name="obj"></param>
/// <param name="t"></param>
/// <param name="path"></param>
/// <returns></returns>
public static object DeSerializeFromXML(this object obj,
Type t, string path) {
XmlSerializer xs = new XmlSerializer(t);
Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
return xs.Deserialize(stream);
}
}
使用 Demo:
定义 SiteUser:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
[Serializable]
public class SiteUser
{
public SiteUser() { }
public int UserId
{ get; set; }
public string UserName
{ get; set; }
public string NickName
{ get; set; }
public string Email
{ get; set; }
}
public class SiteUser
{
public SiteUser() { }
public int UserId
{ get; set; }
public string UserName
{ get; set; }
public string NickName
{ get; set; }
public string Email
{ get; set; }
}
在实际中使用方法:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
private string path = @"d:/siteuser.xml";
private SiteUser sUser {
get {
SiteUser u = new SiteUser()
{
UserId = 1051170,
UserName = "infozero",
NickName = "感恩的心",
Email = "infozero@163.com"
};
return u;
}
}
protected void btnser_Click(object sender, EventArgs e)
{
string text = Serialization.SerializeToString(sUser);
tbvalue.Text = text;
}
protected void btndeser_Click(object sender, EventArgs e)
{
string text = tbvalue.Text;
SiteUser u = text.DeSerializeObject() as SiteUser;
tbname.Text = u.UserName;
tbnick.Text = u.NickName;
tbuid.Text = u.UserId.ToString();
tbemail.Text = u.Email;
}
protected void btntoxml_Click(object sender, EventArgs e)
{
SiteUser u = sUser;
u.SerializeToXML(path , typeof(SiteUser) );
}
protected void btndesxml_Click(object sender, EventArgs e)
{
SiteUser u = new SiteUser();
u = u.DeSerializeFromXML(typeof(SiteUser), path) as SiteUser;
tbname.Text = u.UserName;
tbnick.Text = u.NickName;
tbuid.Text = u.UserId.ToString();
tbemail.Text = u.Email;
}
private SiteUser sUser {
get {
SiteUser u = new SiteUser()
{
UserId = 1051170,
UserName = "infozero",
NickName = "感恩的心",
Email = "infozero@163.com"
};
return u;
}
}
protected void btnser_Click(object sender, EventArgs e)
{
string text = Serialization.SerializeToString(sUser);
tbvalue.Text = text;
}
protected void btndeser_Click(object sender, EventArgs e)
{
string text = tbvalue.Text;
SiteUser u = text.DeSerializeObject() as SiteUser;
tbname.Text = u.UserName;
tbnick.Text = u.NickName;
tbuid.Text = u.UserId.ToString();
tbemail.Text = u.Email;
}
protected void btntoxml_Click(object sender, EventArgs e)
{
SiteUser u = sUser;
u.SerializeToXML(path , typeof(SiteUser) );
}
protected void btndesxml_Click(object sender, EventArgs e)
{
SiteUser u = new SiteUser();
u = u.DeSerializeFromXML(typeof(SiteUser), path) as SiteUser;
tbname.Text = u.UserName;
tbnick.Text = u.NickName;
tbuid.Text = u.UserId.ToString();
tbemail.Text = u.Email;
}
其中生成 XML 文件信息如下:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<?xml version="1.0"?>
<SiteUser xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UserId>1051170</UserId>
<UserName>infozero</UserName>
<NickName>感恩的心</NickName>
<Email>infozero@163.com</Email>
</SiteUser>
<SiteUser xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UserId>1051170</UserId>
<UserName>infozero</UserName>
<NickName>感恩的心</NickName>
<Email>infozero@163.com</Email>
</SiteUser>