locked
Saving Lists of Custon classes in with building windows apps RRS feed

  • Question

  • How can I save a List of a custom class, lets say Person, with the windows app development classes and such? It appears to me that I can't use BinaryFormatter any more, like in the link below:

    http://social.msdn.microsoft.com/Forums/en-US/4e9e9de1-421d-4663-a27b-342d17536fa2/the-easiest-way-how-to-save-classes-to-file?forum=Vsexpressvcs

    An example would help a lot.

    Thursday, December 12, 2013 4:41 PM

Answers

  • Hi,

    You can use ApplicationDataCompositeValue class to save your values to roaming data as a collection.
    Assume you create a Person class like below:

     public class Person
            {
                string Name;
                int Age;
            }
    To save to a composition, use the following code:
     public void write(Person person)
            {
                Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
                Windows.Storage.ApplicationDataCompositeValue composite = new Windows.Storage.ApplicationDataCompositeValue();
                composite["personName"] = person.Name;
                composite["personAge"] = person.Age;
                roamingSettings.Values["personInfo"] = composite;
            }
    To Read a Person object, use the following code:
     public void Read()
            {
                ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)roamingSettings.Values["personInfo"];
                person.Name = composite["personName"];
                person.Age = composite["personAge"];
            }
    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Dave SmitsMVP Friday, December 13, 2013 7:58 AM
    • Marked as answer by Anne Jing Thursday, December 19, 2013 1:35 AM
    Friday, December 13, 2013 3:59 AM

All replies

  • Hi,

    You can save your data by creating JSON file. Newtonsoft can be used to create JSON file from classes and you can use json2csharp to get back your data from the json file.

    Ex: Using JavascriptSerializer class

    sing System;
    using System.Web.Script.Serialization;
    
    public class MyDate
    {
        public int year;
        public int month;
        public int day;
    }
    
    public class Lad
    {
        public string firstName;
        public string lastName;
        public MyDate dateOfBirth;
    }
    
    class Program
    {
        static void Main()
        {
            var obj = new Lad
            {
                firstName = "ABC",
                lastName = "XYZ",
                dateOfBirth = new MyDate
                {
                    year = 1901,
                    month = 4,
                    day = 30
                }
            };
            var json = new JavaScriptSerializer().Serialize(obj);
            Console.WriteLine(json);
        }
    }

    Thursday, December 12, 2013 5:03 PM
  • Hi,

    You can use ApplicationDataCompositeValue class to save your values to roaming data as a collection.
    Assume you create a Person class like below:

     public class Person
            {
                string Name;
                int Age;
            }
    To save to a composition, use the following code:
     public void write(Person person)
            {
                Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
                Windows.Storage.ApplicationDataCompositeValue composite = new Windows.Storage.ApplicationDataCompositeValue();
                composite["personName"] = person.Name;
                composite["personAge"] = person.Age;
                roamingSettings.Values["personInfo"] = composite;
            }
    To Read a Person object, use the following code:
     public void Read()
            {
                ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)roamingSettings.Values["personInfo"];
                person.Name = composite["personName"];
                person.Age = composite["personAge"];
            }
    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Dave SmitsMVP Friday, December 13, 2013 7:58 AM
    • Marked as answer by Anne Jing Thursday, December 19, 2013 1:35 AM
    Friday, December 13, 2013 3:59 AM