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

  • ValOkaforValOkafor USMember, University ✭✭

    Have you considered using JSON.Net?

  • AzamatDAzamatD RUMember

    No, but I use SharpSerilalizer
    namespace SaveObject
    {
    [Activity (Label = "SaveObject", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {

            SaveObject obj;
            protected override void OnCreate (Bundle bundle)
            {
                base.OnCreate (bundle);
    
                var serilizer = new SharpSerializer ();
                SetContentView (Resource.Layout.Main);
                Button button = FindViewById<Button> (Resource.Id.myButton);
                try{
    
                    obj=(SaveObject)serilizer.Deserialize(Assets.Open("SaveFileXML.xml"));
    
                }
                catch{
    
    
                    obj = new SaveObject ();
    
                }
    
                    button.Click += delegate {
                        button.Text = string.Format ("{0} clicks!", obj.CountClicks);
                        obj.CountClicks++;
                    serilizer.Serialize(obj,Assets.Open("SaveFileXML.xml"));
    
                    };
    
            }
        }
        [Serializable]
        class SaveObject
        {
            public int CountClicks{get;set;}
        }
    }
    

    I try to do like that, but when I setuped app on my phone, and when I click on button the app close without messag

  • CheesebaronCheesebaron DKInsider, University mod

    Assets are read-only.

  • AzamatDAzamatD RUMember

    I don't know, however when I try to use file memory it doesn't help
    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;}
        }
    }
    

    If there some way to serialize object without using any libraries, such as in Windows Form on Visual Studio

  • AzamatDAzamatD RUMember

    help people!!!

Sign In or Register to comment.