Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
1.2 kB
1
Indexable
Never
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

// add nameSpase
{
    public class Serrialization<SerrializeClass>
    {

        private readonly string _path;
        private XmlSerializer xmlSerializer = new XmlSerializer(typeof(SerrializeClass));
        public SerrializeClass SaveClass;

        public Serrialization(string path = "saveClass.xml")
        {
            _path = path;
        }
        public void Serrialize(SerrializeClass saveClass)
        {
            if (File.Exists(_path))
                File.Delete(_path);

            using (FileStream fs = new FileStream(_path, FileMode.OpenOrCreate))
            {
                xmlSerializer.Serialize(fs, saveClass);
            }
            Console.WriteLine("Serialize compleate");
        }

        public SerrializeClass DeSerrialize()
        {

            using (FileStream fs = new FileStream(_path, FileMode.OpenOrCreate))
            {
                if (fs.Length != 0)
                    SaveClass = (SerrializeClass)xmlSerializer.Deserialize(fs);
            }
            Console.WriteLine("Deserialize compleate");
            return SaveClass;
        }
    }
}