序列化与反序列化工具

这个工具里包含了XML和二进制两种序列化方式。实际使用的时候,发现xml序列化的内容竟然比二进制要小。也许是因为二进制序列化文件中包含了一些程序集的信息吧。

using System;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace MK
{
    
public abstract class Serialize
    {
        
public static string XmlString(object o)
        {
            
using (StringWriter sw = new StringWriter())
            {
                XmlSerializer xs 
= new XmlSerializer(o.GetType());
                xs.Serialize(sw, o);
                
string _temp = sw.ToString();
                sw.Close();
                sw.Dispose();
                
return _temp;
            }
        }
        
public static void XmlSave(string filename, object o)
        {
            
using (FileStream fs = new FileStream(filename, FileMode.Create))
            {
                XmlSerializer xs 
= new XmlSerializer(o.GetType());
                xs.Serialize(fs, o);
                fs.Flush();
                fs.Close();
                fs.Dispose();
            }
        }

        
public static MemoryStream BinaryStream(object o)
        {
            BinaryFormatter bf 
= new BinaryFormatter();
            MemoryStream ms 
= new MemoryStream();
            ms.Seek(
0, SeekOrigin.Begin);
            bf.Serialize(ms, o);
            ms.Seek(
0, SeekOrigin.Begin);
            
return ms;
        }

        
public static void BinarySave(string filename, object o)
        {
            
using (FileStream fs = new FileStream(filename, FileMode.Create))
            {
                BinaryFormatter bf 
= new BinaryFormatter();
                bf.Serialize(fs, o);
                fs.Flush();
                fs.Close();
                fs.Dispose();
            }
        }


        
public static T XmlDeSerialize<T>(string xmlString)
        {
            XmlSerializer xs 
= new XmlSerializer(typeof(T));
            StringReader sr 
= new StringReader(xmlString);
            
object o = xs.Deserialize(sr);
            
return (T)o;
        }
        
public static T XmlFileDeSerialize<T>(string xmlfile)
        {
            
using (StreamReader sr = new StreamReader(xmlfile))
            {
                
string s = sr.ReadToEnd();
                T t 
= XmlDeSerialize<T>(s);
                sr.Close();
                sr.Dispose();
                
return t;
            }
        }
        
public static T BinaryDeSerialize<T>(Stream s)
        {
            BinaryFormatter bf 
= new BinaryFormatter();
            
object o = bf.Deserialize(s);
            
return (T)o;
        }
        
public static T BinaryDeSerialize<T>(string filename)
        {
            FileInfo fi 
= new FileInfo(filename);
            
if (fi.Exists)
            {
                FileStream fs 
= fi.OpenRead();
                BinaryFormatter bf 
= new BinaryFormatter();
                
object o = bf.Deserialize(fs);
                fs.Close();
                fs.Dispose();
                
return (T)o;
            }
            
else
            {
                
return default(T);
            }
        }
    }
}

 

反序列化的时候可指定类型参数以返回指定的类型。当然返回object然后强制转换也是可以的,但是出于.net类型安全的初衷,还是使用类型参数感觉比较稳妥些。

序列化一个对象然后存储成文件形式或入库,需要的时候提取内容返回对象本身并作进一步操作,是个很美的事情。

转载于:https://www.cnblogs.com/muse/articles/1399178.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值