zoukankan      html  css  js  c++  java
  • XML、二进制、SOAP的序列化

    目的

    将对象序列化之后,可以以文件的形式保存下来。

    区别

      XML 二进制 SOAP
    序列化类 XmlSerializer BinaryFormatter SoapFormatter
    SerializableAttribute 标记 不需要 需要 需要
    ISerializable 接口 不需要 需要 需要
    无参构造函数 必须有 不需要 不需要
    被序列化的数据成员属性 public all all
    序列化后的大小

    实现

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Runtime.Serialization.Formatters.Soap;
    using System.Xml.Serialization;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main( string[] args )
            {
                SerializeByXml();
                SerializeByBinary();
                SerializeBySoap();
            }
    
            static void SerializeByXml()
            {
                string filePath = @"b:\XmlSerializer.dat";
    
                if( !File.Exists( filePath ) )
                {
                    List<Product> list1 = new List<Product>();
                    for( int i = 1; i <= 10; i++ )
                        list1.Add( new Product { ProductID = i, ProductName = "产品名称_xml_" + i } );
    
                    XmlSerializer xsSerialize = new XmlSerializer(typeof (List<Product>));
                    using( Stream fsWrite = new FileStream( filePath, FileMode.Create, FileAccess.Write, FileShare.None ) )
                    {
                        xsSerialize.Serialize( fsWrite, list1 );
                        fsWrite.Close();
                    }
                }
    
                List<Product> list2;
                XmlSerializer xsDeserialize = new XmlSerializer( typeof( List<Product> ) );
                using( Stream fsRead = new FileStream( filePath, FileMode.Open, FileAccess.Read, FileShare.None ) )
                {
                    list2 = (List<Product>)xsDeserialize.Deserialize( fsRead );
                    fsRead.Close();
                }
                foreach( Product p in list2 )
                {
                    Console.WriteLine( p );
                }
            }
    
            static void SerializeByBinary()
            {
                string filePath = @"b:\BinarySerializer.dat";
    
                if( !File.Exists( filePath ) )
                {
                    List<Product> list1 = new List<Product>();
                    for( int i = 1; i <= 10; i++ )
                        list1.Add( new Product { ProductID = i, ProductName = "产品名称_binary_" + i } );
    
                    BinaryFormatter xsSerialize = new BinaryFormatter();
                    using( Stream fsWrite = new FileStream( filePath, FileMode.Create, FileAccess.Write, FileShare.None ) )
                    {
                        xsSerialize.Serialize( fsWrite, list1 );
                        fsWrite.Close();
                    }
                }
    
                List<Product> list2;
                BinaryFormatter xsDeserialize = new BinaryFormatter();
                using( Stream fsRead = new FileStream( filePath, FileMode.Open, FileAccess.Read, FileShare.None ) )
                {
                    list2 = (List<Product>)xsDeserialize.Deserialize( fsRead );
                    fsRead.Close();
                }
                foreach( Product p in list2 )
                {
                    Console.WriteLine( p );
                }
            }
    
            static void SerializeBySoap()
            {
                string filePath = @"b:\SoapSerializer.dat";
    
                if( !File.Exists( filePath ) )
                {
                    List<Product> list1 = new List<Product>();
                    for( int i = 1; i <= 10; i++ )
                        list1.Add( new Product { ProductID = i, ProductName = "产品名称_soap_" + i } );
    
                    SoapFormatter xsSerialize = new SoapFormatter();
                    using( Stream fsWrite = new FileStream( filePath, FileMode.Create, FileAccess.Write, FileShare.None ) )
                    {
                        xsSerialize.Serialize( fsWrite, list1.ToArray() );
                        fsWrite.Close();
                    }
                }
    
                List<Product> list2;
                SoapFormatter xsDeserialize = new SoapFormatter();
                using( Stream fsRead = new FileStream( filePath, FileMode.Open, FileAccess.Read, FileShare.None ) )
                {
                    list2 = new List<Product>( (Product[])xsDeserialize.Deserialize( fsRead ) );
                    fsRead.Close();
                }
                foreach( Product p in list2 )
                {
                    Console.WriteLine( p );
                }
            }
        }
    
        [Serializable]
        public class Product
        {
            public int ProductID { get; set; }
            public string ProductName { get; set; }
    
            public override string ToString()
            {
                return string.Format("ProductID:{0}\t\tProductName:{1}", ProductID, ProductName);
            }
        }
    }
    
    注意点
    SOAP不能直接序列化List<T>这种类型的集合,但可以序列化数组[],所以序列化List<T>.ToArray()对象;
    反序列化时,先强制转换为数组,然后再用数组构造List<T>
     
  • 相关阅读:
    class11_创建新的输出字段P2
    class10_创建新的输出字段
    class09_高级过滤数据
    Markdown高级语法
    class08_过滤数据
    class07_查询数据
    class06_插入数据
    class05_操纵表
    class04_创建表02
    class03_Create a new table by SQL
  • 原文地址:https://www.cnblogs.com/freemantc/p/1672616.html
Copyright © 2011-2022 走看看