• Serialization


    例一:Windows Forms程序设计  P493 序列化基础

    1、流

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    using System.IO;
    
    namespace Test1
    {
        class Program
        {
            static string s = "你好hbhbice";
            static int n = 257;
    
            static void Main(string[] args)
            {
                WriteToStream();
            }
    
            static  void WriteToStream()
            {
                ///写入流中
                byte[] byte1 = UnicodeEncoding.Unicode.GetBytes(s);
                byte[] byte2 = BitConverter.GetBytes(n);
                
                //新建一个内存流
                using (Stream stream = new MemoryStream())
                {
                    stream.Write(byte1, 0, byte1.Length);
                    stream.Write(byte2, 0, byte2.Length);
    
                    //将流的位置重置于起点
                    stream.Seek(0, SeekOrigin.Begin);
    
                    //Read from stream
                    byte[] byte3 = new byte[stream.Length - 4];
                    byte[] byte4 = new byte[4];
    
                    stream.Read(byte3, 0, byte3.Length);
                    stream.Read(byte4, 0, byte4.Length);
    
                    Console.WriteLine(UnicodeEncoding.Unicode.GetString(byte3) + "\t" + BitConverter.ToInt32(byte4,0));
                }
            }
        }
    }
    
    效果图:

    image

    2、

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    using System.IO;
    
    namespace Test2
    {
        class Program
        {
            static void Main(string[] args)
            {
                string s = "你好,hello.";
                int n = 257;
                
                using (Stream stream = new MemoryStream())
                {   
                    //写入流中
                    StreamWriter sw = new StreamWriter(stream);
                    sw.WriteLine(s);
                    sw.WriteLine(n);
                    sw.Flush();
    
                    //将流的位置重置到起点
                    stream.Seek(0, SeekOrigin.Begin);
    
                    //从流中读取
                    StreamReader reader = new StreamReader(stream);
                    string s2 = reader.ReadLine();
                    int n2 = int .Parse (reader.ReadLine());
    
                    Console.WriteLine(s2 + "\t\t" + n2);
                   
                }
            }
        }
    }
    

    image

    3、

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    using System.IO;
    
    
    namespace Test3
    {
        class Program
        {
            static void Main(string[] args)
            {
                string s = "你好,  hello.";
                int n = 257;
    
                using (Stream stream = new MemoryStream ())
                {
                    BinaryWriter bw = new BinaryWriter(stream);
                    bw.Write(s);
                    bw.Write(n);
                    bw.Flush();
    
                    stream.Seek(0, SeekOrigin.Begin);
    
                    BinaryReader br = new BinaryReader(stream);
    
                    string s2 = br.ReadString();
                    int n2 = br.ReadInt32();
    
                    Console.WriteLine(s2+"\t\t"+n2);
    
                }
            
            }
    
        }
    }
    

    image

    4、

    MyData.cs

    ///program.cs
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Runtime.Serialization.Formatters.Soap;
    using System.IO;
    
    namespace Test4
    {
        class Program
        {
            /// <summary>
            /// 将一个类序列化成xml
            /// </summary>
            /// <param name="args"></param>
            static void Main(string[] args)
            {
                MyData md = new MyData();
                
                using (Stream stream = new FileStream (@"C:\temp\mydata.xml",FileMode.Create ))
                {
                    IFormatter formatter = new  SoapFormatter ();
                    formatter .Serialize (stream ,md);
    
                    stream.Seek(0, SeekOrigin.Begin);
    
                    MyData data2 = (MyData)formatter.Deserialize(stream);
    
                    Console.WriteLine(data2.Name + "   " + data2.Age);
                }
            }
        }
    }
    image

    5、

    //这个工作引用了Test4
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using  Test4;
    
    namespace Test5
    {
        class Program
        {
            /// <summary>
            /// 把一个对像序列化为二进制文件
            /// </summary>
            /// <param name="args"></param>
            static void Main(string[] args)
            {
                
                using (Stream stream = new FileStream (@"C:\temp\ice.dat",FileMode.Create ))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    MyData md = new MyData();
                    Console.WriteLine(md.ToString()+"\t"+md.Name +"\t"+md .Age );
                    bf.Serialize(stream, md);
                    stream.Seek(0, SeekOrigin.Begin);
    
                    MyData data2 = (MyData)bf.Deserialize(stream);
    
                    Console.WriteLine();
                    Console.WriteLine(data2.ToString() + "\t" + data2.Name + "\t" + data2.Age);
                    
                }
            }
        }
    }

    image

  • 相关阅读:
    ASP.NET 4.0 与 Entity Framework 4第四篇Entity Framework在三层架构中的使用
    ASP.NET 4.0 与 Entity Framework 4第一篇采用ModelFirst 开发方式创建数据库
    ASP.NET 4.0 与 Entity Framework 4第二篇使用Entity Framework 进行CRUD操作
    ASP.NET 4.0 与 Entity Framework 4第三篇使用Entity Framework调用存储过程
    Unity3D4.* NGUI制作动态字库
    Unity3D内存释放
    1112 Stucked Keyboard (20 分)(map)【回顾】
    1116 Come on! Let's C (20 分)(hash散列)
    1108 Finding Average (20 分)(字符串处理)
    1113 Integer Set Partition (25 分)(排序)
  • 原文地址:https://www.cnblogs.com/hbhbice/p/1725022.html
Copyright © 2020-2023  润新知