Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
1.0 kB
2
Indexable
Never
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;
            DeSerrialize();
        }
        public void Serrialize(SerrializeClass saveClass)
        {
            if (File.Exists(_path))
                File.Delete(_path);

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

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