Serializing Objects to Isolated Storage in Silverlight 2

[Cross posted from: SilverlightForBusiness.net ]

Silverlight 2 does not have a BinaryFormatter like the full framework does so you have to take a different approach to serializing data to Isolated Storage.

However there is a simple solution by using the DataContractSerializer.  You will need to add a reference to System.Runtime.Serialization.  This will serialize the object to an Xml format and is what Silverlight uses when you add a service reference.

So if we have an object called Person, we can use the DataContract and DataMember attributes to mark it up as follows:

[DataContract]
public class Person
{
    [DataMember]
    public int PersonId { get; set; }
    [DataMember]
    public string Name { get; set; }
    
}

You can then use the DataContractSerializer as follows to save Person data to Iso storage:

Person p = new Person() { PersonId = 1, Name = "Fred" };
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForSite())
{
    IsolatedStorageFileStream stream = store.CreateFile("Person.dat");
    DataContractSerializer serializer = new DataContractSerializer(typeof(Person));
    serializer.WriteObject(stream, p);
    stream.Close();
}
MessageBox.Show("Data Saved");

And the following to read it:

Person p;
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForSite())
{
    IsolatedStorageFileStream stream = store.OpenFile("Person.dat", FileMode.Open);
    DataContractSerializer serializer = new DataContractSerializer(typeof(Person));
    p = (Person)serializer.ReadObject(stream);
    stream.Close();
}
MessageBox.Show("Data Loaded");
TextBlock1.Text = p.Name + " " + p.PersonId;

If you want to make this more generic you could create a generic method as follows:

private void SaveData<T>(T dataToSave,string fileName)
{
    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForSite())
    {
        IsolatedStorageFileStream stream = store.CreateFile(fileName);
        DataContractSerializer serializer = new DataContractSerializer(typeof(T));
        serializer.WriteObject(stream, dataToSave);
        stream.Close();
    }
}

Then you can call it as follows:

SaveData<ObservableCollection<Person>>(people, "People.dat");
SaveData<Person>(person, "Person.dat");

Cheers

Ian

posted @ Thursday, November 27, 2008 11:26 AM

Print

Comments on this entry:

# re: Serializing Objects to Isolated Storage in Silverlight 2

Left by watch tv at 12/13/2008 12:43 AM
Gravatar
Great code.

# Soma cube.

Left by Soma intimates. at 7/8/2009 6:45 AM
Gravatar
Suicide with soma. Soma muscle relaxant. Temp soma.

# Viagra.

Left by Viagra. at 7/8/2009 10:55 PM
Gravatar
Generic viagra. Your viagra onlinne guide.

# Get viagra.

Left by Best price for generic viagra. at 7/9/2009 10:31 PM
Gravatar
Viagra. Women s viagra. Get viagra. Female viagra.

# Vicodin.

Left by Buy vicodin online without. at 7/10/2009 10:37 PM
Gravatar
Vicodin side effects. Vicodin. Difference between vicodin and lortab. Vicodin detox. Abuse of vicodin.

# Tramadol 180 cod.

Left by Tramadol. at 7/11/2009 10:35 PM
Gravatar
Tramadol side effects.

# Xanax.

Left by Buy generic xanax information. at 7/12/2009 10:59 PM
Gravatar
Side effects of xanax. Ativan xanax valuim trial pack. Buy xanax online without a prescription. Xanax. Uhi foundation forums xanax cheap phentermine.

# Will percocet show up in a drug test.

Left by Percocet. at 7/13/2009 11:05 PM
Gravatar
Percocet online. Dosages for percocet. How long is percocet in your system.

# How do i get off percocet.

Left by Buy percocet online. at 7/14/2009 11:06 PM
Gravatar
Percocet 93-490. Percocet. Percocet dosage. Order percocet online.

# Tramadol 93.

Left by Tramadol overdose. at 7/15/2009 11:07 PM
Gravatar
Cheap tramadol. Tramadol. Cheapest tramadol. Tramadol cheap no rx.

# Phentermine.

Left by Order phentermine phentermine online. at 7/17/2009 1:14 AM
Gravatar
Phentermine online. Low cost phentermine. Phentermine obesity. Phentermine. Phentermine pill. Buy phentermine mg.

# Vicodin for the soul.

Left by Vicodin side effects. at 7/18/2009 12:34 AM
Gravatar
Vicodin. Purchase vicodin. Dangers of vicodin. Vicodin addiction. Vicodin withdrawal symtoms.

# Home mortgage lender.

Left by Mortgage interest rate. at 1/12/2010 1:14 AM
Gravatar
Mortgage calculator.

# Chico soma.

Left by Chico soma. at 1/12/2010 2:03 PM
Gravatar
Chico soma.

# Order phentermine 37.5mg without prescription.

Gravatar
Order phentermine 37.5mg without prescription.

# Mixing cocaine and phentermine.

Left by Mixing cocaine and phentermine. at 1/22/2010 9:50 AM
Gravatar
Mixing cocaine and phentermine.

# Cheap phentermine low prices buy now.

Left by Cheap phentermine low prices buy now. at 1/25/2010 9:22 AM
Gravatar
Cheap phentermine low prices buy now.

# Adipex generic form phentermine lowest prices.

Left by Buy generic phentermine bloghoster. at 1/25/2010 11:38 PM
Gravatar
Is there a generic for phentermine. Generic phentermine. Generic phentermine overnight.
Comments have been closed on this topic.
«September»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789