序列化与反序列化

扩展相关类的方法:
Serialization
    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);
        }

    }


使用 Demo:
定义 SiteUser:

SiteUser
    [Serializable]
    
public class SiteUser
    {
        
public SiteUser() { }

        
public int UserId
        { 
getset; }

        
public string UserName
        { 
getset; }

        
public string NickName
        { 
getset; }

        
public string Email
        { 
getset; }

    }


在实际中使用方法:

在页面中使用示例
        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;
        }


其中生成 XML 文件信息如下:

SiteUser.XML
<?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>







posted on 2009-12-09 22:51  感恩的心  阅读(468)  评论(0编辑  收藏  举报

导航