四種序列化器

1.二進制序列化器json

/// <summary>
        /// 二進制序列化器
        /// </summary>
        public static void BinarySerialize()
        {
            //使用二進制序列化對象
            string fileName = Path.Combine(Constant.SerializeDataPath, @"BinarySerialize.txt");//文件名稱與路徑
            using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
            {//須要一個stream,這裏是直接寫入文件了
                List<Programmer> pList = DataFactory.BuildProgrammerList();
                BinaryFormatter binFormat = new BinaryFormatter();//建立二進制序列化器
                binFormat.Serialize(fStream, pList);
            }
            using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
            {//須要一個stream,這裏是來源於文件
                BinaryFormatter binFormat = new BinaryFormatter();//建立二進制序列化器
                //使用二進制反序列化對象
                fStream.Position = 0;//重置流位置
                List<Programmer> pList = (List<Programmer>)binFormat.Deserialize(fStream);//反序列化對象
            }
        }

2.Soap序列化器ui

public static void SoapSerialize()
        {
            //使用Soap序列化對象
            string fileName = Path.Combine(Constant.SerializeDataPath, @"SoapSerialize.txt");//文件名稱與路徑
            using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
            {
                List<Programmer> pList = DataFactory.BuildProgrammerList();
                SoapFormatter soapFormat = new SoapFormatter();//建立二進制序列化器
                //soapFormat.Serialize(fStream, list);//SOAP不能序列化泛型對象
                soapFormat.Serialize(fStream, pList.ToArray());
            }
            using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
            {
                SoapFormatter soapFormat = new SoapFormatter();//建立二進制序列化器
                //使用二進制反序列化對象
                fStream.Position = 0;//重置流位置
                List<Programmer> pList = ((Programmer[])soapFormat.Deserialize(fStream)).ToList();//反序列化對象
            }
        }

3.xml序列化器code

public static void XmlSerialize()
        {
            //使用XML序列化對象
            string fileName = Path.Combine(Constant.SerializeDataPath, @"Student.xml");//文件名稱與路徑
            using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
            {
                List<Programmer> pList = DataFactory.BuildProgrammerList();
                XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Programmer>));//建立XML序列化器,須要指定對象的類型
                xmlFormat.Serialize(fStream, pList);
            }
            using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
            {
                XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Programmer>));//建立XML序列化器,須要指定對象的類型
                //使用XML反序列化對象
                fStream.Position = 0;//重置流位置
                List<Programmer> pList = pList = (List<Programmer>)xmlFormat.Deserialize(fStream);
            }
        }

4.json序列化器orm

相關文章
相關標籤/搜索