欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c# 序列化和反序列化

程序员文章站 2022-06-16 08:17:20
...
using System;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Runtime.Serialization.Formatters.Binary;

namespace serializeAndnot
{
    [DataContract]
    class Book
    {
        [DataMember]
        public int ID = 777;
        [DataMember]
        public string name = "king";
        [DataMember]
        public string[] strArray = new string[3]{ "aa","bb","cc"};//数组
        [DataMember]
        public Test mytest = new Test(); //对象
    }
    [DataContract]
    class Test
    {
        [DataMember]
        public string str01 = "kk";
        [DataMember]
        public string str02 = "hh";
    }
    [DataContract]
    class Mouse
    {
        [DataMember]
        public int ID ;
        [DataMember]
        public string name;
        [DataMember]
        public string[] strArray;
        [DataMember]
        public Test mytest;
    }

    public class Shop
    {
        public string name = "hh";
        public int level = 5;
    }

   public class City
    {
        public string name = string.Empty;
        public int level = 0;
    }

    [Serializable]
    public class Shop2
    {
        public string name = "hhh";
        public int level = 50;
    }

    [Serializable]
    public class Shop3
    {
        public string name = string.Empty;
        public int level = 0;
    }

    class Program: XmlTextReader
    {
        static void Main(string[] args)
        {
           // JsonSerializeTest();
           // JsonDeserialize();
          //  XmlSerialize();
           // XmlDeserialize();
           // BinarySerialize();
            BinaryDeserialize();
            Console.ReadKey();
        }

        //Json序列化
        static void JsonSerialize()
        {
            Book book = new Book();
            DataContractJsonSerializer jsonContract = new DataContractJsonSerializer(typeof(Book));
            using (MemoryStream ms = new MemoryStream())
            {
                //将指定数据序列化为JSON数据,并将JSON数据写入stream
                jsonContract.WriteObject(ms, book);
                string str = Encoding.UTF8.GetString(ms.ToArray());
                FileStream fs = new FileStream("E:/json.txt",FileMode.OpenOrCreate);
                fs.Write(ms.ToArray(),0, ms.ToArray().Length);
                fs.Close();
                Console.WriteLine(str);
            }
        }
        //json反序列化
        static void JsonDeserialize()
        {
            string str = "{\"ID\":777,\"mytest\":{\"str01\":\"kk\",\"str02\":\"hh\"},\"name\":\"king\",\"strArray\":[\"aa\",\"bb\",\"cc\"]}";
            DataContractJsonSerializer jsonContract = new DataContractJsonSerializer(typeof(Mouse));
            byte[] byteArray = Encoding.UTF8.GetBytes(str);
            using (MemoryStream ms = new MemoryStream(byteArray))
            {
                Mouse t2 = jsonContract.ReadObject(ms) as Mouse;
                Console.WriteLine(t2.ID);
                Console.WriteLine(t2.name);
                Console.WriteLine(t2.strArray[1]);
                Console.WriteLine(t2.mytest.str01);
                Console.WriteLine(t2.mytest.str02);
            }
        }
        //xml序列化
        static void XmlSerialize()
        {
            XmlSerializer xml = new XmlSerializer(typeof(Shop));
            Shop shop = new Shop();
            using (FileStream fs = new FileStream("E:/xmlContent.xml", FileMode.OpenOrCreate))
            {
                xml.Serialize(fs, shop);
            }
        }
        //xml反序列化
        static void XmlDeserialize()
        {
            XmlSerializer xml = new XmlSerializer(typeof(Shop));
            using (FileStream fs = new FileStream(@"E:/xmlContent.xml", FileMode.Open))
            {
                XmlReader reader = new XmlTextReader(fs);
                Shop shop = xml.Deserialize(reader) as Shop;//反序列化时保持类的一致性
                City city = new City();
                city.level = shop.level;
                city.name = shop.name;
                Console.WriteLine(city.level);
                Console.WriteLine(city.name);
            }
        }
        //二进制序列化
        static void BinarySerialize()
        {
            Shop2 shop = new Shop2();
            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = new FileStream("E:/二进制.txt", FileMode.OpenOrCreate);
            bf.Serialize(fs, shop);
            fs.Close();
        }
        //二进制反序列化
        static void BinaryDeserialize()
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = new FileStream(@"E:/二进制.txt", FileMode.Open);
            Shop2 shop2 = bf.Deserialize(fs) as Shop2; //反序列化时保持类的一致性
            Shop3 shop3 = new Shop3();
            shop3.level = shop2.level;
            shop3.name = shop2.name;
            Console.WriteLine(shop3.level);
            Console.WriteLine(shop3.name);
            fs.Close();
        }

    }
}