hbhbice

导航

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

posted on 2010-04-30 13:53  hbhbice  阅读(309)  评论(0编辑  收藏  举报