Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more: , +
(Windows 8 Apps, C#)
Hi,
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
Updated 8-May-13 5:20am
v4
Comments
Sergey Alexandrovich Kryukov 8-May-13 11:07am    
Why do you mix up serialization and Metro together. Ever heard of separation of concerns?
—SA
Sergey Alexandrovich Kryukov 8-May-13 11:12am    
Platform? Language? Anything?... You need to tag relevant information.
—SA
Abhinav Varma 8-May-13 12:10pm    
ya..sorry for that..I am using C# on .NET
Sergey Alexandrovich Kryukov 8-May-13 12:54pm    
OK, then my answer will fully solve your problem, please see.
—SA
Abhinav Varma 19-May-13 6:08am    
would you help me solving the problem in my last post sir..

Please see my comment to the question.

The best method is perhaps the Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

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: http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/index.html[^].

—SA
 
Share this answer
 
Comments
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)
Sergey Alexandrovich Kryukov 18-May-13 1:48am    
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.
—SA
Quote:
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[^]
 
Share this answer
 
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:
C#
[DataContract(Namespace = "http://www.abcdefg.com")]
    internal class Record
    {
        private double n1;
        private BitmapImage bimg;

        internal Record(double n1, BitmapImage bimg)
        {
            this.n1 = n1;
            this.bimg = bimg;
        }
        [DataMember]
        internal BitmapImage image
        {
            get { return bimg; }
            set { bimg = value; }
        }

        [DataMember]
        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:

C#
private async void save_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.List;
            openPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
            openPicker.FileTypeFilter.Add(".jpg");
            StorageFile file = await openPicker.PickSingleFileAsync();
            IRandomAccessStream imageStream = await file.OpenReadAsync();
            bitmapImage = new BitmapImage();
            bitmapImage.SetSource(imageStream);
            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);
                stream1.WriteTo(x);
            }
}
</string>


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..
XML
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900