Serilization of object using XML

How I can save, object obj of class objectik to XML file, then open it

 objectik obj ;
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            SetContentView (Resource.Layout.Main);
            Button button = FindViewById<Button> (Resource.Id.myButton);


            obj = new objectik ();//creating new object
            obj.clicks = 0;


            button.Click += delegate {

                button.Text = string.Format ("{0} clicks!", obj.clicks);
                obj.clicks++;
                     };
                }


         }

                [Serializable]
                public class objectik 
                {


                    public int clicks {get;set;}
               }

Answers

  • AzamatDAzamatD RUMember
    edited March 2015
            objectic obj;
            private void button1_Click(object sender, EventArgs e)
            {
                XmlSerializer forrmatter = new XmlSerializer(typeof(objectic));
                if ((System.IO.File.Exists("Persons.xml")))
                {
                    using (FileStream fs = new FileStream("Persons.xml", FileMode.OpenOrCreate))
                    {
                        obj = (objectic)forrmatter.Deserialize(fs);
                    }
                    label1.Text = obj.count.ToString();
                }
                else
                {
                    obj = new objectic();
                }
    
    
                obj.count++;
                using (FileStream fs = new FileStream("Persons.xml", FileMode.OpenOrCreate))
                {
    
    
                    forrmatter.Serialize(fs, obj);
                }
    
            }
    
            [Serializable]
            public class objectic
            {
                public int count { get; set; }
            }
        }
    }
    
    
                button.Click += delegate {
    
                    button.Text = string.Format ("{0} clicks!", obj.clicks);
                    obj.clicks++;
                    using(FileStream fs =new FileStream(sdcard+"XMLSave.xml",FileMode.OpenOrCreate))
                    {
                        forrmater.Serialize(fs,obj);
                    }
    
                         };
                    }
    
             }
    
                    [Serializable]
                    public class objectik
                    {
                     public int clicks {get;set;}
                   }
    

    When I check app on phone, the application didn't load and give error

  • EdwardAbramianEdwardAbramian USMember ✭✭

    Can you give error detail?

    By the way, you don't need [Serialize] attribute for xlm serialization.

  • AzamatDAzamatD RUMember

    Not any error and crash/ Application only close
    this code
    public class MainActivity : Activity
    {

                SaveObject obj;
                protected override void OnCreate (Bundle bundle)
                {
                    base.OnCreate (bundle);
    
                    SetContentView (Resource.Layout.Main);
    
    
    
                    Button button = FindViewById<Button> (Resource.Id.myButton);
    
    
    
    
    
    
                        button.Click += delegate {
                        XmlSerializer formmater =new XmlSerializer(typeof(SaveObject));
                        if(File.Exists("object.xml"))
                        {
                            using(FileStream fs =new FileStream("object.xml",FileMode.OpenOrCreate))
                            {
                                obj =(SaveObject)formmater.Deserialize(fs);
                            }
    
                        }
                        else{
    
                            obj =new SaveObject();
                        }
    
    
    
                            button.Text = string.Format ("{0} clicks!", obj.CountClicks);
                            obj.CountClicks++;
    
                        using(FileStream fs =new FileStream("object.xml",FileMode.OpenOrCreate))
                        {
    
                            formmater.Serialize(fs,obj);
                        }
    
    
    
    
                        };
    
                }
    
            }
    
            class SaveObject
            {
                public int CountClicks{get;set;}
            }
        }
    
  • AzamatDAzamatD RUMember

    When I click on button1

  • AzamatDAzamatD RUMember

    Help people!!!

Sign In or Register to comment.