Esempio n. 1
0
        static void Main(string[] args)
        {
            //personinfo pi=new personinfo("martin", 22);
            serialize s = new serialize();
            //s.savedata(pi);
            personinfo pnew = new personinfo();

            pnew.showdata();
            pnew = s.getdata();
            pnew.showdata();
        }
 public void savedata(personinfo pi)
 {
     try
     {
         fs = new FileStream(@"C:\files\personinfo.xml", FileMode.Create, FileAccess.Write);
         XmlSerializer xs = new XmlSerializer(typeof(personinfo));
         xs.Serialize(fs, pi);
         Console.WriteLine("Data serialized");
     }
     catch (IOException ex)
     {
         Console.WriteLine(ex.Message);
     }
     finally
     {
         fs.Close();
     }
 }
        public personinfo getdata()
        {
            personinfo pi = null;

            try
            {
                fs = new FileStream("c:\\files\\personinfo.xml", FileMode.Open, FileAccess.Read);
                XmlSerializer xs = new XmlSerializer(typeof(personinfo));
                pi = (personinfo)xs.Deserialize(fs);
                Console.WriteLine("Successfully deserialized");
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                fs.Close();
            }
            return(pi);
        }