序列化与反序列化

多个对象序列化和反序列化

namespace Dome
{
    //声明对象
    public class Account
    {
        public int UserID { get; set; }
        public string Username { get; set; }
        public string UserPass { get; set; }
    }

    class Param
    {
        //数组赋值
        static Account[] accounts = 
            {
                 new Account(){UserID=1,Username="Name1",UserPass="Pass1"},
                 new Account(){UserID=2,Username="Name2",UserPass="Pass2"},
                 new Account(){UserID=3,Username="Name3",UserPass="Pass3"}
            };

        static void Main(string[] args)
        {
            SerializerMethod();
            DeSerializerMethod();
        }

        static string SavePath = @"D:\XmlSerializer.xml";
        //将对象序列化到XML文档中
        static XmlSerializer xmls = new XmlSerializer(typeof(Account[]));

        //序列化
        public static void SerializerMethod()
        {
            using (TextWriter tw = new StreamWriter(SavePath))
            {
                xmls.Serialize(tw, accounts);
                tw.Close();
            }
        }

        //反序列化
        public static void DeSerializerMethod()
        {
            using (TextReader tr = new StreamReader(SavePath))
            {
                Account[] DeSerialzer = xmls.Deserialize(tr) as Account[];
                if (DeSerialzer != null && DeSerialzer.Length > 0)
                {
                    for (int i = 0; i < DeSerialzer.Length; i++)
                        Console.WriteLine("id={0}\t UserID={1},UserName={2},UserPass={3}", i, DeSerialzer[i].UserID, DeSerialzer[i].Username, DeSerialzer[i].UserPass);
                }
            }
            Console.ReadKey();
        }
    }
}

 

 

二进制方式,可以使用BinaryFormatter 类来以二进制格式将对象或整个连接对象图形序列化和反序列化

 

namespace Demo2
{
   [Serializable]
    public class Account
    {
        public int UserID { get; set; }
        public string Username { get; set; }
        public string Address { get; set; }
    }
    class param
    {
        static void Main(string[] args)
        {
            Account[] accounts = 
            {
                new Account(){UserID=1,Username="科比",Address="湖人"},
                new Account(){UserID=2,Username="韦德",Address="热火"},
                new Account(){UserID=3,Username="姚明",Address="火箭"}
            };

            BinaryFormatter format = new BinaryFormatter();
            string savePath = @"D:\BinSerialzer.xml";
            //序列化
            using (FileStream fsw = new FileStream(savePath, FileMode.Create, FileAccess.Write))
            {
                format.Serialize(fsw,accounts);
                fsw.Close();
            }
            
            //反序列化
            using (FileStream fsr = new FileStream(savePath, FileMode.Open, FileAccess.Read))
            {
                Account[] deserialzer = format.Deserialize(fsr)as Account[];
                if (deserialzer != null && deserialzer.Length > 0)
                {
                    for (int i = 0; i < deserialzer.Length; i++)
                    {
                        Console.WriteLine(" UserID={0},UserName={1},Address={1}",accounts[i].UserID,accounts[i].Username,accounts[i].Address);
                    }
                }
            }
            Console.ReadKey();
        }
    }
}

 

你可能感兴趣的:(xml)