Click here to Skip to main content
13,095,921 members (53,698 online)
Rate this:
Please Sign up or sign in to vote.
See more: , +
(Windows 8 Apps, C#)
I have a number of Images(.jpg) and Audio-files(.mp3) in each object. I would like to store these objects in separate files and retrieve the content later. I searched for Object Serialization, but could not find a right example for Windows 8 apps.
So, what are the ways of saving the objects to files in Windows 8 apps(C#).
If it is Object Serialization, please give a sample piece of code or a link to it.
Posted 8-May-13 4:46am
Updated 8-May-13 5:20am
Why do you mix up serialization and Metro together. Ever heard of separation of concerns?
Platform? Language? Anything?... You need to tag relevant information.
Abhinav Varma 8-May-13 12:10pm
ya..sorry for that..I am using C# on .NET
OK, then my answer will fully solve your problem, please see.
Abhinav Varma 19-May-13 6:08am
would you help me solving the problem in my last post sir..
Sergey Alexandrovich Kryukov 19-May-13 13:14pm
Which is?
Abhinav Varma 19-May-13 13:17pm
the one in the bottom with the name "Solution 4"....
Sergey Alexandrovich Kryukov 19-May-13 13:49pm
Don't try to serialize "ImageSource". What is that, the whole image. Do something else, pass the binary file and serialize just the file name.

First of all, you should not post questions as "solutions". It will only give you abuse reports and down-votes. You should post it as a part of question, via "Improve question".

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Please see my comment to the question.

The best method is perhaps the Data Contract:[^].

Please see my past answers where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

The answers are related to .NET, highly recommended. If you don't use any .NET languages, please don't blame me: you did not even mention the language you are using.

For C++ (not C++/CLI), for example, I would recommend to look for boost serialization:[^].

Abhinav Varma 18-May-13 1:41am
Thanks for your help,
but I am unable to use XmlWriter, and Write objects through it.

My Class will look like this :
public class ExClass
List<Image> imgList = new List<Image>();

I could actually store all the images to a folder as StorageFile and retrieve them.
But I want the files NOT to be opened by any others or any other applications.
I want the files, only to be accessed and opened by what the application I write.

Can you suggest a method or
would you please write few sample lines code for the above solution you advised, if you dont mind...
(I am a student, and new to .Net)
What do you mean "unable"? How can I teach you doing programming job in a Quick Question?
If you want to use file exclusively, you can 1) use isolated storage (read about it), 2) keep files open during application lifetime.
This could be a separate unrelated question.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

So, what are the ways of saving the objects to files in Windows 8 apps(C#).

Have a look at this Tip:
Windows 8 App - Data storage in files using Windows.Storage namespace[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

I am getting an Exception:
An exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in System.Runtime.Serialization.dll but was not handled in user code

Additional information: Type 'Windows.UI.Xaml.Media.ImageSource' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

at serializer.WriteObject(stream1, record1)

The class is:
[DataContract(Namespace = "")]
    internal class Record
        private double n1;
        private BitmapImage bimg;
        internal Record(double n1, BitmapImage bimg)
            this.n1 = n1;
            this.bimg = bimg;
        internal BitmapImage image
            get { return bimg; }
            set { bimg = value; }
        internal double OperandNumberOne
            get { return n1; }
            set { n1 = value; }
        public override string ToString()
            return string.Format("Record: {0} and an image: {1}", n1, bimg.ToString());

and the Serialization is done here:

private async void save_Click(object sender, RoutedEventArgs e)
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.List;
            openPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
            StorageFile file = await openPicker.PickSingleFileAsync();
            IRandomAccessStream imageStream = await file.OpenReadAsync();
            bitmapImage = new BitmapImage();
            ImgUIelement.Source= bitmapImage;
            MemoryStream stream1 = new MemoryStream();
            //Serialize the Record object to a memory stream using DataContractSerializer.
            DataContractSerializer serializer = new DataContractSerializer(typeof(Record));
            Record record1 = new Record(1, bitmapImage);
            serializer.WriteObject(stream1, record1);   // EXCEPTION HERE
            tbx_OP.Text = "Written to Stream:" + record1.ToString();
            FileSavePicker fileSavePicker = new FileSavePicker();
            fileSavePicker.DefaultFileExtension = ".bla";
            fileSavePicker.FileTypeChoices.Add("Bla Files", new List<string> { ".bla" });
            fileSavePicker.SuggestedFileName = "New Bla File";
            fileSavePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            var sfile = await fileSavePicker.PickSaveFileAsync();
            using (Stream x = await sfile.OpenStreamForWriteAsync())
                x.Seek(0, SeekOrigin.Begin);

My requirement is to serialize an object consisting a List of Images and List of StorageFiles into a File, in my Windows Store App. I am trying to find intially whether it is possible to serialize a Single Image.
If possible could anyone please provide the sample code for a class like this..
public class ExClass
    List<Image> imgList = new List<Image>();
    List<StorageFile> audList = new List<StorageFile>();

I did'nt find any examples for serializing Types other than the Strings,Int,Double..Primitive ones.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 19 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100