Thursday 2 June 2016

c# form serialization

private void clear_all()
        {
            foreach (var ctrl in this.Controls)
            {
                if (ctrl is TextBox)
                {
                    ((TextBox)ctrl).Clear();
                }
            }

            uxid.Focus();
        }

---------------------------------------------------------
using System.Runtime.Serialization.Formatters.Binary;

public static void save()
        {
            var formatter = new BinaryFormatter();

            using (var stream = new FileStream(file_path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                formatter.Serialize(stream, _students);
            }
        }

---------------------------------------------------------
        private static void retrive_student()
        {
            var formatter = new BinaryFormatter();

            using (var stream = new FileStream(file_path, FileMode.OpenOrCreate, FileAccess.Read))
            {
                _students = (List<student>) formatter.Deserialize(stream);
            }
        }

-------------------------------------------------------------
using System.Xml.Serialization;

const string FilePath = "students.xml";
        private List<Student> _students;

        public XmlStudentManager()
        {
            //deserialize here...
            var serializer = new XmlSerializer(typeof(List<Student>),
                                               new Type[] { typeof(Student) });
            using (var stream = new FileStream(FilePath, FileMode.OpenOrCreate))
            {
                _students = serializer.Deserialize(stream) as List<Student>;
            }
        }

--------------------------------------------------------------

        public void Save()
        {
            //serialize here...
            var serializer = new XmlSerializer(typeof(List<Student>),
                                               new Type[] { typeof(Student) });
            using (var stream = new FileStream(FilePath, FileMode.OpenOrCreate))
            {
                serializer.Serialize(stream, _students);
            }
---------------------------------------------------------------

No comments:

Post a Comment